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

I recently worked in a multinational company that manages its Customer Relationship Management and sales data, using Salesforce. One day, I was tasked with calculating the lead age in whole days.

As a solution, I discovered that this Salesforce tool provides us with the function named “TRUNC”, which helps to remove the decimal part of the age and shows the age as whole days.

In this Salesforce tutorial, we will learn how to use the TRUNC() function in Salesforce to truncate a number to a specified number of digits. We will also understand its syntax and a few useful examples.

What is the TRUNC() Function in Salesforce?

The TRUNC() function in Salesforce is a mathematical function used to perform calculations. In basic terms, the TRUNC() function truncates a number to a specified number of digits.

In simple words, we can say that the TRUNC() function in Salesforce is used to round down a numerical value to the nearest whole number or a given number of decimal places. Basically, it truncates the decimal part of the number and returns the integer part.

In addition, the TRUNC() function only supports numeric data types, such as Integers, Decimals, and Currency. However, an error will result if you try to use a text, date, or other non-numeric data type.

The syntax of the TRUNC() function is as follows:

TRUNC(number,num_digits)

Here:

  • Number: It specifies the value that you want to truncate.
  • num_digits: It specifies the number of decimal places to which you want to round the number, but it is an optional parameter. If not specified, the function rounds numbers to the nearest whole number.

For a better understanding of how the TRUNC function works, let’s look at an example.

As I mentioned above, I need to find the lead age in whole days. Therefore, I will utilise the Salesforce TRUNC() function for this. The formula is as given below:

TRUNC(Age__c, 0)

With this, we learned the syntax of the TRUNC() function and an example. We’ll now look at how to use the TRUNC() function in Salesforce.

Use the TRUNC() Function in Salesforce

The following steps show how to use the TRUNC() function in Salesforce.

  1. Open Salesforce Lightning mode. Next, click the “Gear Icon” in the top right corner of the page. Click Setup from the dropdown menu to continue.
TRUNC Function in Salesforce
  1. The setup page opens when you select the Setup option. Here, the “Object Manager” is found in the Navigation Bar next to the Home tab. Click on it.
TRUNC Function in Salesforce Example
  1.  The object manager page opens when you click it. There are numerous objects seen here. Use the Quick Find search field to find the item on which we want to use the TRUNC() function.
    • In this instance, I look for the “Product” object because I need to calculate the shipping cost based on the product’s rounded weight.
    • Now, click the “Product” object to move to the product object page.
TRUNC Function in Salesforce Lightning
  1. Select Field & Relationship from the menu. Click the “New” button to create a field.
TRUNC Function in Salesforce Lightning Example
  1. The field type page opens when you click the new button. The page contains a wide variety of field types. In this case, I chose the Formula field type, as I want to use the TRUNC function in the advanced formula.
    • Click the “Next” button to move on to the next step.
Salesforce TRUNC Function
  1. The choose output type screen appears when you click the following button. Enter the information now by filling out the “Field Label” and the “Field Name” I typed “Shipping Charges” into the field label.
    • Choose the “Output Data Type for the field in which we want to store the result. In this case, I check the radio button next to the “Currency” data type to return the formula result as currency, and I also set the “Decimal Place” to 3.
    • Move to the next step by clicking the “Next” button.
Salesforce TRUNC Function Example
  1. Go to the advanced formula editor and enter the TRUNC() function formula there. In this instance, I want to use the TRUNC function to calculate the shipping cost based on the product’s rounded weight. The formula is as follows:
TRUNC(Weight__c, 1)
  • Here, we round the product’s weight to 1 decimal place using the TRUNC() function.
  • This function removes excess decimal digits and truncates the result to 1 decimal place.
  • To validate the formula and make sure there are no mistakes, click the “Check Syntax” option.
  • Optionally, we may define the “Description,” “Help text,” and “Handle the empty field” if we like.
  • Click on the “Next” button to go to the security setup for the formula field.
TRUNC() Function in Salesforce
  1. In this step, establish Field-Level Security. Now choose the profiles to which we wish to grant field-level security edit access to this field. If field-level security is not added, the field will be hidden from all profiles.
    • As I want it to be visible to all profiles, I have chosen “Visible” in this instance. After that, click the “Next” button located at the top.
Salesforce Lightning TRUNC Function Example
  1. Add the custom field to the Page Layout after setting up the field-level security. Pick the page layout that has this field in it. If we do not select a layout, the field will not appear on any pages.
    • To save the formula field, click the “Save” button.
How to use TRUNC Function in Salesforce Lightning

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

  • Open the Products Tab and create a new product with the field Weight.
  • The Shipping Charges field, which displays the output of the TRUNC() function, appears when you click on the Details Section.
How to use TRUNC Function in Salesforce Lightning Example

As a result, we now understand how to use the TRUNC function in Salesforce.

Conclusion

In conclusion, we have learned that the Salesforce function TRUNC() helps handle numerical data and truncates a number to a specified number of digits.

We also learned that we can apply this function in several real-world scenarios, including “Price Book Discount”, “Case Response Time”, “Lead Conversion Rate”, etc.

Additionally, we understand the Salesforce TRUNC() function syntax and approach for using it.

You may like to read:

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.