ABS() Function in Salesforce | Calculate absolute value in Salesforce

In this Salesforce tutorial, we will learn about the ABS() function and how we can use it to calculate the absolute value of numerical data. In addition, we will also focus on its syntax and some practical examples.

While working in a Sales Organization that uses Salesforce as a customer relationship management tool to track their sales. I got a task to calculate the difference between target sales and actual sales.

Here I found an ABS() function in Salesforce that helped me to calculate the absolute difference. Now, let us discuss in detail, how to use the Salesforce ABS() function to calculate the absolute value of a number in Salesforce Lightning as well as Salesforce Classic.

What is ABS() function in Salesforce

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

This ABS() function is used to calculate the absolute value of a number. The absolute value represents the magnitude of a number without considering its sign.

In layman’s terms, the ABS() function is used to calculate the absolute number which gives the positive value of a number as a result without considering whether the original number was positive or negative.

Moreover, we should be kept one thing in mind the ABS() 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 ABS() function in Salesforce

The syntax of the ABS() function is given below:

ABS(number/field/formula)

The number/field/formula parameter is the value for which we wish to determine the absolute 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 ABS() function.

Suppose, we want to calculate the absolute value of the numeric field value called “Net Profit” and for this, we use the ABS() function and calculate it. The formula is as follows:

ABS(Net Profit)

With this, we have understood the ABS() function with its syntax, important points, and example. Now, we will move and try to understand the steps to calculate the absolute value in Salesforce Lightning.

How to calculate the absolute value in Salesforce Lightning

Here, are the steps to calculate the absolute value in Salesforce Lightning:

1: First, log in to Salesforce Lightning mode and navigate to the right corner of the page, then click on the “Gear Icon” (⚙). Now, from the dropdown menu (⬇) click on the “Setup”.

ABS Function in Salesforce
Setup for ABS Function in Salesforce

2: From the left side of the menu, click on “Object Manager”.

ABS Function in Salesforce Example
Object Manager for ABS Function in Salesforce Example

3: Search for the object in the search bar where we wish to utilize the ABS() function. In this case, I search for the “Opportunity” object as I want to track the sales.

4: Now, click on the “Opportunity” object and it will redirect us to the Opportunity Page.

ABS Function in Salesforce Lightning
Opportunity Object for ABS Function in Salesforce Lightning

5: From the left side of the page click on the “Field & Relationship” option.

6: Click on the “New” button to create a new field where we can use the “ABS() function”.

How to calculate the absolute value in Salesforce Lightning
Calculate the absolute value in Salesforce Lightning

7: It will redirect to the “Choose the field type” page, now check the radio button in front of the “Formula” data type.

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

ABS Function in Salesforce Lightning Example
ABS Function in Salesforce Lightning Example

9: Enter the “Field Label” for the formula and the unique “Field Name” will be automatically populated. In this case, I entered the field label named “Absolute Sales Variance”.

10: Choose the “Data Type” for the field for which we want to calculate the absolute value. Make sure the data type must be of a numeric type such as “Number“, “Currency“, etc.

In this case, I check the radio button in front of the “Currency” to set it as the formula return type.

11: Now, to move to the next step click on the “Next” button.

Calculate absolute value in Salesforce Lightning
Calculate absolute value in Salesforce Lightning

12: Move to the advance formula editor, and enter the formula that uses the ABS() function to calculate the absolute value.

In this case, I want to calculate the absolute value of the sales variance i.e. the difference between target sales and actual sales using the ABS() function. Here, is the formula:

ABS(Target_Sales__c - Actual_Sales__c)
  • In this formula, we subtract the “Actual_Sales__c” field from the “Target_Sales__c” field.
  • Then we use the ABS() function with it to calculate the absolute value of the difference between actual sales and target sales.

13: Click on the “Check Syntax” button to validate the formula and ensure that there are no errors.

14: Optionally, we can “Handle the empty field” as well as we can define the “Description” and “Help text” if we want.

15: Click on the “Next” button to move ahead.

Calculate absolute value in Salesforce
Calculate absolute value in Salesforce

16: Choose the appropriate field “Level Security”. Here, I check the “Visible” checkbox as I want it to be visible to all. Then, click on the “Next” button.

Calculate absolute value in Salesforce Example
Field Level Security to calculate absolute value in Salesforce

17: Now, select the appropriate “Page Layout” where we want to be visible.

18: Lastly, click on the “Save” button to save the formula field.

Calculate absolute value in Salesforce Lightning Example
Add Page Layout to calculate absolute value in Salesforce Lightning

We can use the formula field once we’ve created it. Let’s look at an example:

  • Open the Opportunity Tab and create a New Opportunity with the fields Target Sales and Actual Sales.
  • After that, click on the Details section, and here the Absolute Sales Variance field appears which shows you the result of the ABS() function.
How to calculate the absolute value in Salesforce Lightning Example
Calculate the absolute value in Salesforce Lightning Example

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

Read ACOS() Function in Salesforce | Calculate inverse cosine value in Salesforce

How to calculate the absolute value in Salesforce Classic

Here, are the steps to calculate the absolute value in Salesforce Classic:

1: From the Salesforce Lightning to open your Salesforce Classic edition. Click on Avtar and then click Switch to Salesforce Classic.

ABS Function in Salesforce Classic
Switch to Salesforce Classic

2: After that, click on the Setup option.

ABS Function in Salesforce Classic Example
Setup to for ABC function in Salesforce Classic Example

3: Now, scroll down to the Build section. Then under Customize, select the object for whom field we want to use the ABS() function. In this case, I select the “Cases” object as I want to track the response time.

4: Click on the “Cases” dropdown and select the “Fields” option to get the option of the new field.

Calculate absolute value in Salesforce Classic
Calculate the absolute value of the Case in Salesforce Classic

5: Now scroll down to the “Custom Fields & Relationships” and click on the “New” button to create a new formula field.

Calculate absolute value in Salesforce Classic Example
New formula field for Casein Salesforce Classic

6: It will redirect to the “Choose the field type” page, now check the radio button in front of the “Formula” data type.

7: To proceed to the next step, click on the “Next” button.

How to calculate the absolute value in Salesforce Classic
Calculate the absolute value in Salesforce Classic

8: The unique “Field Name” will be filled out automatically when you enter the “Field Label” for the formula. I entered “Response Time Variance” into the field labeled in this instance.

9: Select the “Data Type” for field that we want to calculate the absolute value. Make sure the data type must be of a numeric type such as “Number“, “Currency“, etc.

In this case, I check the radio button in front of the “Number” to set it as the formula return type. I also set the decimal place to 0.

10: Now click the “Next” button to go to the next step.

How to calculate the absolute value in Salesforce Classic Example
Calculate the absolute value in Salesforce Classic Example

11: Move to the advance formula editor, and enter the formula that uses the ABS() function to calculate the absolute value.

In this case, I want to calculate the absolute value of the response time variance i.e. the difference between target response time and initial response time using the ABS() function. Here, is the formula:

ABS(Target_Response_Time__c - Initial_Response_Time__c)
  • In this formula, we subtract the “Target_Response_Time__c” field from the “Initial_Response_Time__c”  field.
  • Then we use the ABS() function with it to calculate the absolute value of the difference between the target response time and the initial response time.

12: To check the formula and make sure there are no mistakes, click the “Check Syntax” option.

13: Optionally, we can define the “Description” and “Help text” as well as “Handle the empty field” if we prefer.

14: Click on the “Next” button to move ahead.

Calculate the absolute value in Salesforce Classic
Calculate the absolute value in Salesforce Classic

15: Select the relevant “Level Security” field. I have selected “Visible” in this case because I want it to be visible to everyone. Following that, select “Next” to move ahead.

Calculate the absolute value in Salesforce Classic Example
Select Field Level Security in Salesforce Classic

16: Now, select the appropriate “Page Layout” where we want this field to be visible.

17: Lastly, click on the “Save” button to save the formula field.

Calculate the absolute value using Salesforce Classic Example
Set Page Layout in Salesforce Classic Example

We can use the formula field once we’ve created it. Let’s look at an example:

  • Open the Case Tab and create a New Case with the fields Target Response Time and Initial Response Time.
  • On Save, it will redirect us to the Detail page, and here the Response Time Variance field appears which shows us the result of the ABS() function.
How to calculate the absolute value using Salesforce Classic
Calculate the absolute value using Salesforce Classic

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

Conclusion

With this, we have concluded that the Salesforce function ABS() is a great tool for working with numerical data. In addition, we have learned practical examples such as calculating differences, net changes, handling negative numbers, etc.

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

You may like to read the following articles: