Salesforce SECOND Function

In this Salesforce tutorial, we will learn about the Salesforce SECOND function and its use cases. This Second function is executable in the DateTime fields of Salesforce Lightning and Salesforce Classic.

What is a SECOND function in Salesforce

In Salesforce, the SECOND function extracts the second component as numbers from a datetime field. The second value returns in the form of a number from 0 to 60.

This function only displays the seconds in a time field based on the value of the Datetime field. For example, SECOND(TIMEVALUE(“01:30:20.125”)) returns 20.

Syntax for SECOND function in Salesforce

The following syntax is used for the SECOND function in Salesforce:

SECOND(time)

In the above syntax replace the time value with a Date/Time field from which you want to extract the seconds or the value like TIMENOW().

Considerations:

There are some considerations that we should be aware of while using the SECOND function in Salesforce.

  • The SECOND function should be executed with the Datetime field.
  • While using the SECOND function in a formula field, make sure the formula field is of the correct type.
  • Formulas that use the SECOND function will return a numeric result.

Use the SECOND function in Salesforce Lightning

In this example, we will create a custom field with the help of the SECOND function. We will create this field to track the time between the Account creation and activation. The output field will return the remaining seconds to activate the account in the number format.

For example, if the remaining time is 2 minutes the output will be 120.

1. Navigate to the Setup page of Salesforce Lightning, and for that click on the settings icon that is on the upper left side of the screen then in the dropdown click on the Setup option.

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

3. In the Object Manager, click on the Account object or select the object in which you have to create the custom formula field.

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

Salesforce Lightning Second function

5. In this step, select the field data type as Formula and click on the Next button.

Second function use case in Salesforce Lightning

6. In this window, we will define the output of the formula field by entering the below fields.

In this example, I have named the output in the Field Label as Seconds Remaining for activation and the Field Name will be auto-filled according to the entered Field Label.

In the Formula return type, select the option Number and click on the Next button.

Create a custom formula field using Second function in Salesforce

7. In this step, enter the below formula in the formula editor.

IF(  Activation_Time__c   > NOW(), 
    24 * 60 * 60 * (  Activation_Time__c   - NOW()), 
    null
)

The condition that we have applied in the above formula is as follows:

  • The IF conditional calculates the remaining time in seconds till the Activation time, and it returns null if the date is not in the future.
  • Activation_Time__c > NOW() checks if the value of the Activation_Time__c field is greater than the current date and time (NOW()).
  • 24 * 60 * 60 * converts the time difference to seconds by multiplying it by the number of seconds in a day.
  • ( Activation_Time__c – NOW()) calculates the remaining time in seconds until the future date and time.
  • If none of the above conditions is True, the output will be Null.

8. After entering the formula click on the button Check Syntax to validate the formula and on successful validation click on the Next button.

Salesforce Lightning Second function use case

9. Select the profiles, that can have access to this field. If you want to give access to all users then activate the Visible checkbox and click on the Next button.

Salesforce Lightning Second function execution

10. In this step, activate the page layouts to make this field visible on the page layouts. This field will be visible only on the activated page layouts. After activating the page layouts, click on the Save button.

Create a formula field with Second function in Salesforce

As we click on the Save button the formula field will be created in the Account object. Now we will open an account record to view the output of the formula field that we have created using the SECOND function.

Salesforce Lightning Second function Output field

We can see in the above image the output formula field Seconds Remaining for Activation returns the number of seconds that is calculated between the current time and activation time.

Here the time returned as 236 seconds, which is approximately 4 minutes.

In this way, we have successfully executed the SECOND function in Salesforce Lightning by creating a custom formula field with the help of the SECOND function.

How to use the SECOND function in Salesforce Classic

In this example, we will use the SECOND function to create a formula field in the Cases object that will calculate the time difference between the Start and End time of the case and return the output in a number of seconds. i.e. if the time difference is 3 minutes the output will be 180.

With the help of this field, we can track the time taken to resolve that specific case.

Now to create a custom formula field in Salesforce Classic, follow the below steps.

1. On the Home page of Salesforce Classic, click on the Setup tab.

2. In the Setup window, go to the Build section on the left sidebar click on the Customize dropdown then select Case -> Fields.

Salesforce Classic Second function use case

3. In the account setup, scroll down to the section Custom Fields & Relationships and click on the New button.

4. In this step, select the field data type as Formula and click on the Next button.

Create a formula field with Second function in Salesforce Classic

5. Now we will define the output of the formula field by entering the below fields.

Enter the output field name in the Field Label. In this example, I have entered the Field Label as Case duration in seconds and the Field Name will be auto-filled according to the entered Field Label.

In the Formula Return type, select the option Number and click on the Next button.

Salesforce Classic Second function Output field

8. In this step, we will enter the below formula in the formula editor.

IF(
    NOT(ISBLANK( Start_Time__c )) && NOT(ISBLANK( End_Time__c )),
    (End_Time__c - Start_Time__c ) * 24 * 60 * 60,
    NULL
)

We have defined the above formula in the following way:

  • IF conditional evaluates whether a condition is true or false in the formula.
  • NOT(ISBLANK( Start_Time__c )) && NOT(ISBLANK( End_Time__c )), checks if both the “Start Time” and “End Time” fields are not blank.
  • (End_Time__c – Start_Time__c ) * 24 * 60 * 60, calculates the time difference between the “End Time” and “Start Time” fields then the result is multiplied by 24 (hours), 60 (minutes), and 60 (seconds) to convert the time difference from days to seconds.
  • Null will be the field output if the fields “Start Time” and “End Time” are blank.

9. Now we will validate the entered formula, and for that click on the button Check Syntax. After successful validation i.e. no error in the formula then click on the Next button.

Salesforce classic Second function Output field formula

10. In this step, select the profiles that can view this field. To select all user profiles activate the Visible checkbox and click on the Next button.

Salesforce Classic create a field with Second function

11. Activate the page layouts to make this field visible on the page layouts and after this click on the Save button.

Salesforce Classic custom Formula field with Second function

As we click on the Save button the formula field will be saved in the Cases object. Now open a case record to check the output of the formula that you have created using the SECOND function.

Salesforce Classic Second function field Output

We can view the field Case duration in seconds showing the number of seconds that is calculated from the difference between the datetime fields Start Time and End Time. The output field returns the number of seconds 300 which is approximately 5 minutes.

In this way, we successfully executed the SECOND function in Salesforce Classic with the help of a custom formula field.

Conclusion

In this Salesforce tutorial, we have discussed the two different use cases of the SECOND function in Salesforce Lightning and Salesforce Classic. In the above examples, we have successfully created formula fields using the SECOND function.

In addition to this, we also learned about the use of the SECOND function in Salesforce and learned the syntax of the SECOND function later we discussed the considerations that we should be aware of while using the SECOND function in Salesforce.’

You may like to read: