Salesforce FormatDuration Function

In this tutorial, we are going to learn about the FormatDuration function in Salesforce, and we will also discuss a use case of the FormatDuation function using a custom formula field in Salesforce.

Salesforce provides a wide range of Functions that are helpful in Formula fields and Report formulas that are used to get specific records in reports.

What is FormatDuration() Function in Salesforce

In Salesforce, FormatDuration is a DateTime/time function that returns the duration between the two date/time fields. When we use the Formatduration() function in a formula, if the difference between the two date/time fields is 2 days and 30 minutes then we will get the output as 02:00:30:00 in the DD:HH:MM: SS format.

There are a few considerations that we should be aware of while using the FormatDuration() function in Salesforce fields and reports formulas.

  • Depending on your use case, you might need to integrate the FormatDuration() function with other functions or expressions.
  • In the expression of the Formatduration function, always use the dateTime/time field; any other field will give a Syntax error.

Syntax of the Salesforce Formatduration() Function

The syntax of the Salesforce FormatDuration() function is written in the following way.

 FORMATDURATION(numSeconds[, includeDays] | dateTime/time, dateTime/time) 

In the above syntax, numSeconds is supposed to be replaced with optional days, or the difference between times or DateTime as HH:MI: SS.

This includeDays parameter is a Boolean value that indicates whether or not to include the days in the formatted duration. If days are included it will be TRUE else it will be False.

The dateTime field in the above syntax is used as an expression, and both datetime fields are supposed to be replaced by a datetime/time field.

The output type of this function is taken as text, and the output format is DD:HH:MM: SS.

Where D = day, H = hours, M = minutes, and S = Seconds.

How to use the FormatDuration function in Salesforce Lightning

In the below steps, we will discuss the execution and use case of the FormatDuration function in Salesforce Lightning. In this example, we will create a custom Formula field using the FormatDuration function.

1. Navigate to the Setup page of Lightning, and for that click on the settings icon, and in the dropdown select the Setup option.

Use FormatDuration function in Salesforce

2. On the Setup page of Lightning, click on the Object Manager tab.

Format Duration Function formula field in Salesforce Lightning

3. In the Object Manager setup select an object in which you have to create a formula field using FormatDuration function. In this case, I have selected the object Account.

4. In the selected object click on Fields & Relationships then in the setup of Fields & Relationships, click on the New button.

Salesforce Lightning Use Format duration function

5. In this step, select the Formula data type and click on the