# COS() Function in Salesforce | Calculate cosine value in Salesforce

In this Salesforce tutorial, we will learn the COS() function in Salesforce and how we can use it for calculating the cosine value of numerical data in Salesforce. Moreover, we will focus on the syntax of the COS() function and some practical examples for implementation.

Recently I worked for a company that utilizes Salesforce as a CRM tool. While working here, I was given the responsibility to determine the budget for the upcoming campaign so that the company could decide the total fund requirement.

I discovered the Salesforce COS() function as a solution for this, which enabled me to determine the budget for the upcoming campaign. Let’s now go into more detail about how to use Salesforce Lightning and Salesforce Classic components for using COS() function to determine the cosine value.

## What is COS() function in Salesforce

In Salesforce, we have multiple functions that are used to perform mathematical calculations and the COS() function is one of the mathematical functions that is used to perform trigonometric calculations.

This COS() function in Salesforce is used to calculate the cosine of the given angle or value. In addition, this function always returns a value in radians and degrees.

Moreover, we should have to keep one thing in our mind the COS() function works only on numeric data types such as Decimal, Integer, or Double. If you use it with other data types such as text, date, or other non-numeric data types it will produce an error.

### Syntax and Usage of Salesforce COS() function

The syntax for the COS() function in Salesforce is given below:

``COS(number/field/formula)``

The number/field/formula parameter is the value for which we wish to determine the cosine value. The value specified in the parameter can be a field, a numeric constant, or a formula.

Let’s see an example to clearly understand the concept and syntax of the COS() function.

Suppose, we want to calculate the cosine value of the numeric field value called “Lead Prioritization” and for this, we use the COS() function and calculate it. Here is the formula:

``COS(Feedback_Rating__c/10)+50``

With this, we have understood the COS() function with its syntax, logic, and real-life based examples. Now, we will move ahead and try to figure out the steps to calculate the cosine value in the Salesforce Lightning Component.

## Calculate cosine value using COS() Function in Salesforce Lightning

The steps to determine the cosine value in Salesforce Lightning are as follows:

1: Log in to Salesforce Lightning mode, then click on the “Gear Icon” located in the top right corner of the page. To proceed, select “Setup” from the dropdown menu.

2: From the menu’s left side, click on “Object Manager“.

3: Now, search for the object for which you want to use the COS() function in the search bar. As I want to track the sales performance of the sales lead team. So, in this example, I look up the “Lead” object.

4: Click on the “Lead” object, and it will redirect to the lead page.

5: From the menu on the left-hand side of the page, click on “Field and Relationship”.

6: To create a formula field where we will use the COS() function, click on the “New” button.

7: Now, it redirects to the “Choose the field type” page. Here, select the “Formula” as the data type by checking the radio button next to it.

8: To proceed, click on the “Next” button.

9: After entering the “Field Label” for the formula, the “Field Name” will be filled in automatically. In this instance, I entered “Sales Performance” as the field label.

10: Select the “Data Type” for field on which we want to apply the COS() function. As we know COS() function only accepts the numeric data type select any one of them “Number” or “Currency” as per requirement.

In this case, I choose the “Number” as the formula return type and also set its “Decimal Value” to as I only want it to return the result with 3 decimal positions.

11: Click on the “Next” button to proceed to the next step.

12: To compute “Sales Performance” by using the COS() function formula, enter the formula in the advanced formula editor. The formula is as follows:

``COS(Revenue_Generated__c / Target_Revenue__c)``
• Here, “Sales Performance” is determined by the formula using the “Revenue_Generated__c” and “Target_Revenue__c” fields.
• Firstly, we divide the “Revenue_Generated__c” field value with the “Target_Revenue__c” field value.
• Then, we use the COS() function and pass the resultant value to the function to get the final result.

13: To make sure that there is no mistake in the formula, click on the “Check Syntax” button.

14: Optionally, we may define the “Description”“Help Yext”, and as well as “Handle the empty field” if you like.

15: To continue, click on the “Next” button.

16: Select the appropriate “Level Security” option. In this case, I have selected the “Visible” checkbox as I want it to be visible to everyone. Then, click on the “Next” button from the menu.

17: Now, choose the “Page Layout” that will display your content.

18: To save the advanced formula field, click on the “Save” button.

Once we have set up the formula field, we may use it. Let’s examine an illustration.

• Create a new lead with the Revenue Generated and Target Revenue by opening the “Leads” Tab.
• After that, click on the “Details” section, and here we will see the Sales Performance field the resultant output obtained by using the COS() function.

As a result, we now know how to calculate the cosine value in Salesforce Lightning. Now, we will look at how to calculate the cosine value in Salesforce Classic.

## Calculate the cosine value in Salesforce Classic

The steps to determine the cosine value in Salesforce Classic are as follows:

1: Open your Salesforce Classic edition from Salesforce Lightning. Click on “Avtar” and then click on “Switch to Salesforce Classic”.

2: Next, select “Setup” from the menu.

3: Scroll down and move to the “Build” section and select “Customize” and search for the object on which field you want to use the COS() function.

Here, I choose the “Lead” object as I want to find out the “Hike Chance”.

4: To get the option for creating a new formula field, click on the dropdown adjacent to the “Leads” and choose the “Fields” option.

5: Now scroll down and move to the “Lead Custom Fields & Relationship” section and click on the “New” button to create a formula field.

6: It moves to the “Choose the field type” page, select the “Formula” as the data type by checking up the radio button adjacent to it.

7: Click on the “Next” button to move to the next proceeding step.

8: Enter the “Field Label” for the formula and the unique “Field Name” will be filled out automatically. In this example, I specify the “Hike Chance” as the field label.

9: Next, choose the “Data Type” for the field where we are going to calculate the lead hike chances using the COS function. Make sure that you only choose the numeric data types such as “Number” and “Currency” otherwise it will give an error.

In this case, I select the “Number” as the formula return data type by clicking on the radio button in front of it. Moreover, I adjusted the “Decimal Place” to 1.

10: Now, click on the “Next” button to proceed to the step enter formula.

11: Go to the advanced formula editor and type the formula to compute the cosine value using the COS() function.

In this instance, I want to use the COS function to determine the cosine value of the hike chance based on the performance. The formula is as follows:

``(1 + COS(Performance__c)) / 2``
• Here, we first calculate the performance by passing it to the COS() function and then we add the resultant value to 1.
• Next, we divide the sum obtained by 2 to get the final hike chance of the lead.

12: Click “Check Syntax” to review the formula and ensure there are no errors.

13: If we would like, we may specify the “Description” and “Help text” as well as “Handle the empty field“.

14: To proceed, use the “Next” button.

15: Choose the appropriate “Level Security” field. Because I want it to be visible to everyone, I have chosen “Visible” in this instance. Select “Next” to continue after that.

16: Next, choose the suitable “Page Layout” for the area where we wish to display this field.

17: In order to save the formula field, click the “Save” button.

Once we’ve established the formula field, we may use it. Let’s examine an illustration:

• Create a new lead with the field Performace by opening the Lead tab.
• When we click Save, we are taken to the Detail page, where the Hike Chance field displays the COS() function output.

As a result, we now know how to calculate the cosine value in Salesforce Classic.

## Conclusion

The Salesforce function COS() is useful function for working with numerical data. In addition, we have learned practical examples such as “Campaign Performance Evaluation”, “Lead Prioritization”, and “Budget and Delivery Performance”, etc.

Moreover, we have also understood the concept of the Salesforce COS() function with its syntax and procedure of calculating the cosine value in Salesforce Lightning and Salesforce Classic.

You may also like: