ABS() Function in Salesforce | Syntax, Examples, & Use Cases

We use Salesforce CRM to manage our sales business. We wanted to calculate and store the difference between the Target Sales and Actual Sales in a custom field.

As a Salesforce Admin, I created a Formula Field on the opportunity object. In the formula, I used the ABS() function so that the result always shows the difference as a positive number, whether the salesperson achieved less than the target (negative value) or more than the target (positive value).

This way, the field clearly shows the Sales Gap in Salesforce without confusion of negative numbers.

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

What is the ABS() Function in Salesforce?

In Salesforce, we have multiple formulas and functions used to perform mathematical calculations, and the ABS() function is one of these 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 value of a 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 keep in mind that 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”. For this, we use the ABS() function to 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.

ABS() function in Salesforce: Calculate the Absolute Value in Salesforce

Now, let’s understand how to create and use the ABS() function in Salesforce to calculate the absolute value of numerical data.

  1. Go to the Gear Icon. -> Click on the Setup. -> Select the Object Manager tab. Select the Object on which you want to create the Formula Field. Here, I’m going to create an Opportunity Object.
How to calculate the absolute value in Salesforce Lightning
  1. In this step, we will use a formula to calculate and display the date after adding days to the given date, and display it on the product object. To do this, select the Formula option from the data type and click Next.
ABS Function in Salesforce Lightning Example
  1. Next, provide the Field Label and API Name for the formula field. Then, since we want to display the next date after adding days to the given date, in the  Formula Return Type, select Date and click on the Next button.
Calculate absolute value in Salesforce Lightning
  1. In the formula editor, enter the formula below and click the Next button.
    • 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.
  • Click the “Check Syntax” button to validate the formula and ensure that it contains no errors.
  • Optionally, we can “Handle the empty field” as well as we can define the “Description” and “Help text” if we want.
  • Click on the “Next” button to move ahead.
ABS() Function in Salesforce
  1. Select the Field Level Security for the formula field, and then select the profiles to make the field visible to those profiles. The field will be visible for the profiles selected from the Visible column.
    • After selecting the profiles, click on the Next button.
Calculate absolute value in Salesforce Example
  1. In this step, we must select the field’s visibility on the object’s page layouts. In this case, we have created a formula field for the reports, allowing us to choose not to display it on the page layouts.
    • Uncheck the checkboxes of the page layouts to prevent the field from displaying on them. After this, click on the Save button.
Calculate absolute value in Salesforce Lightning Example
  1. We can use the formula field once it has been created. 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

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.

Conclusion

With this, we have concluded that the Salesforce function ABS() is an excellent tool for working with numerical data. In addition, we have learned practical examples, such as calculating differencesnet changesand handling negative numbers.

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

You may like to read the following articles:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.