Salesforce MILLISECOND Function

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

What is MILLISECOND function in Salesforce

In Salesforce, the MILLISECOND function extracts the millisecond component from a time field and it returns a Milliseconds value in the form of a number that ranges between 0 to 999.

This function only displays the milliseconds in a time field based on the value of the time field. For example, MILLISECOND(TIME VALUE (“05:45:15.225”)) returns 225.

Syntax for MILLISECOND function in Salesforce

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

MILLISECOND(time)

In the above syntax replace the time value with a Time field from which you want to extract the milliseconds.

Considerations:

There are the following considerations while using the MILLISECOND function in Salesforce.

  • In Salesforce, the MILLISECOND function can only be executed with the Time field.
  • This function cannot extract the MILLISECOND component from the datetime or date field.
  • Formulas that use the MILLISECOND function will return a numeric result.

Use the MILLISECOND function in Salesforce Lightning

In this example, we will create a custom field with the help of the MILLISECOND function. We will create this field to track the time between the created time and the activation time of a contact record.

The output field will return the remaining milliseconds to activate the account in the number format. Since the record is activated within just milliseconds after being created, it was not possible to track it in minutes and even seconds.

To create a custom formula field using the MILLISECOND function in Salesforce Lightning follow the below steps.

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

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

3. In the Object Manager, click on the Contact object to create the custom formula field.

Salesforce MILLISECOND function use case

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

Create a custom formula field using the MILLISECOND function

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

Salesforce Lightning MILLISECOND function execution

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 MilliSeconds taken 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.

Salesforce Lightning MILLISECOND function formula output

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

MILLISECOND(Created_at__c ) - MILLISECOND(Activated_at__c)

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

  • MILLISECOND function will extract the milliseconds from the time value of the field Created at and Activated at.
  • The subtraction operator (-) in the formula subtracts the milliseconds of the Created at from the milliseconds of the Activated at. This gives the time difference in milliseconds between the two time fields

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

Create a custom formula field using the MILLISECOND function in Salesforce

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 MILLISECOND function formula field

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.

Millisecond function use case in Salesforce Lightning

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

Salesforce Lightning MILLISECOND function output field

We can see in the above image the output formula field returns the MilliSeconds taken for activation that is calculated between the created time and activation time.

Here the time returned as 561 which is the number of MILLISECONDS taken for the activation of the contact record from creation. This means the time was 11:36:00:561, so we got the extracted millisecond in the output.

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

Use the MILLISECOND function in Salesforce Classic

In this example, we will use the MILLISECOND function to create a formula field in the lead object that will calculate the time difference between the time when the lead was created and the current time, the output field will return the time difference in the number of Milliseconds.

Now follow the below steps to create a custom formula field to create a custom formula field using the MILLISECOND function.

1. Navigate to the Setup page of Classic and for that click on the Setup tab that is on the upper right side of the window.

2. In the Setup page of Salesforce Classic, go to the Build section on the left sidebar and select Customize -> Lead -> Fields.

Salesforce Classic MILLISECOND function execution

3. In the fields setup of the Lead object go to the section Custom Fields and Relationships then click on the New button.

Salesforce Classic MILLISECOND function use case

4. In this step, select the Data Type as Formula and click on the Next button.

5. In this window we will define the output of the formula field.

Enter the output name in the Field Label, like I have entered the name as Last Activity Since and the Field Name will be auto-filled according to the entered Field Label.

After this select the Formula return type as Number then click on the Next button.

Salesforce Classic Custom field with MILLISECOND function

6. Now we will enter the formula below in the Formula editor.

MILLISECOND(TIMENOW() ) - MILLISECOND(Last_Modified_at__c )

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

  • The MILLISECOND function will extract the milliseconds component from a time value.
  • MILLISECOND(TIMENOW() ) function extracts the millisecond component from the current timestamp obtained by TIMENOW.
  • MILLISECOND(Last_Modified_at__c ), function extracts the millisecond component from the timestamp stored in the “Last_Modified_at__c” field.
  • The subtraction () will subtract the current time (TIMENOW) from the time when the lead was last modified.

7. After entering the formula, click on the button Check Syntax to validate the formula and check errors. On successful validation click on the Next button.

Salesforce Classic formula field with MILLISECOND function

8. In this step, enable the profiles that can be allowed to view this field. If you want to enable all profiles then activate the Visible checkbox and click on the Next button.

Create a custom formula field using the MILLISECOND function in Salesforce Classic

9. Activate the page layouts to make this field visible on the page layouts. This field will not be visible on inactivated page layouts. At last click on the Save button.

Extract Millisecond from Time field in Salesforce

As we click on the Save button the formula field will be created in the Lead object. Now we will open a lead record to get the output from the created formula field.

Salesforce Classic MILLISECOND function output field

In the Lead’s record, we can see that the formula fields return output as 702 and that is the number of milliseconds passed since the lead was last modified. For example, if the time was 2:42:30:702 then we got the extracted millisecond in the output and this field will update the time when you refresh the page.

In this way, we have successfully executed the MILLISECOND function in Salesforce Classic by creating a custom formula field.

Conclusion

In this Salesforce tutorial, we have learned about the use cases of the MILLISECOND()  function with the help of custom formula fields in Salesforce Lightning and Salesforce Classic respectively.

Along with this we also discussed the uses, syntax, and considerations that we must be aware of while executing the MILLISECOND() function in the formula of fields and reports in Salesforce.

You may like to read: