Salesforce NOW Function

In this Salesforce tutorial, we will learn about the NOW function in Salesforce and its use cases in Salesforce. This function is executable in the formula fields, reports, and validation rules of Salesforce Lightning and Salesforce Classic.

What is NOW function in Salesforce

In Salesforce, the NOW() function is a date and time function that returns the current date and time in the organization’s time zone. The NOW() function is generally used in formulas, validation rules, and other areas to reference the current date and time.

Syntax for NOW() function

The following expression is used as syntax for the NOW function in Salesforce:

NOW()

The Now() function does not take any expression and value and it returns a date/time representing the current moment.

Considerations for using NOW() function in Salesforce

There are some considerations that should be followed while using the NOW() function.

  • It is not required to enter a value in the NOW() function.
  • Do not remove parentheses while using this function.
  • Instead of a Date field, use the Date/time field in the NOW() function.
  • Use addition and subtraction operators with a NOW() function and other date/time fields to return a number, representing the number of days.
  • Use Number, Subtraction, and Addition operators with the NOW() function to return Date and Time.

How to use NOW() function in Salesforce Lightning

In this example, we will create a formula field using the NOW() function that calculates the time elapsed (in hours) since the account was last updated.

Now in the below steps, we will create a custom formula field in Lightning and for that login to your developer account and follow the below steps.

1. Navigate to the setup page of Salesforce Lightning and for that click on the settings icon at the top and from the options in the dropdown click on the option Setup.

Salesforce Lightning Now function execution

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

3. In the Object Manager setup select an object to create a custom formula field. In this example, I have selected the standard object Account.

Create a custom field using Now function in Salesforce Lightning

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

Salesforce Lightning Formula field with NOW function

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

Salesforce Lightning Now Function

6. In this window we have to define the output of the formula field by entering the required fields.

In this example, I have labeled the output Field label as Hours Since Last Update and the Field Name will be auto-filled according to the entered Field label.

In the Formula Return Type select the option Number since we want to calculate the time difference in hours.

Now function output in Salesforce Lightning

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

IF(ISBLANK(LastUpdateDateTime__c), 0, ROUND((NOW() - LastUpdateDateTime__c) * 24, 2))

Let’s understand the conditions of the used formula in the below points.

  • IF(ISBLANK(LastUpdateDateTime__c), 0 function checks if the custom date/time field LastUpdateDateTime__c is blank. If it is blank, the formula returns 0.
  • The digit 24 used in the formula multiplies the result by 24 converting the time difference from days to hours.
  • ROUND(…… 2)) function rounds the calculated time difference to two decimal places.
  • The field LastUpdateDateTime__c is a custom date/time field used in the formula.

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

Salesforce Lightning Now function syntax validation

9. In this step, select the specific profiles to give them access to this field. If you want to give access to all fields then activate the Visible checkbox and click on the Next button.

Salesforce Lightning Now function field validation

10. In the last step, make sure to activate the page layouts to make this field visible on those page layouts, and after that click on the Save button.

Salesforce Lightning Custom formula field using Now function

As we click on the Save button the custom formula field will be created in the object selected above. Now open a record in that object and there you can check the field that we created using the NOW() function.

Salesforce Custom formula field using Now function in Lightning

In the above, we can view the field Hours Since Last Update returning the hours from the difference between the current time “NOW()” and the Date time field Last Update Date Time.

In this way, we can use the NOW() in a Salesforce Lightning with the help of a custom formula field.

How to use NOW() function in Salesforce Classic

In this example, we will create a custom formula field using the NOW() functions that will return the number of days since the Lead is open. This field will help in improvising the sales pipeline of the org.

To create a custom formula field in Salesforce Classic, navigate to the Home page of Classic.

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

2. In the Setup window, go to the left sidebar and select Customize -> object -> Fields. In this example, I have selected the object Lead to create a custom formula field.

Salesforce Classic NOW function

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

Custom formula field with NOW function in Salesforce

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

Salesforce Classic NOW function custom field

5. In this step, we will define the output of the custom formula field by entering the following fields.

  • Enter the relevant name for the Field Label. In this example, I have entered the field as Open Since Days which is in respect to Leads records.
  • The Field Name will be auto-filled according to the entered Field Label.
  • Select the Formula Return Type as Number and click on the Next button.
Salesforce Classic NOW function formula field output

6. In this step, we will enter the formula using the NOW() function in the Formula editor.

CASE(Status,
    "Open", ROUND(NOW() - CreatedDate, 0),
    null
)

In the above formula the logic we have used is as follows:

  • The CASE function, which is a conditional statement in Salesforce formulas and in this formula it checks the value of the Status.
  • If the lead status is “Open” this formula computes the exact number of days the lead has been open by determining the difference between the current date and time (NOW()) and the date and time when the lead was created (CreatedDate).
  • The ROUND() function is applied to ensure the result is a whole number of days, with zero decimal places.

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

Salesforce Classic NOW function Formula validation

8. To establish the field-level security for this field, activate the user profiles that can access this field. If you have to select all profiles then activate the Visible checkbox and click on the Next button.

Salesforce Classic Now function Custom formula field

9. In this step, select page layouts to make this field visible on the page layouts and this field will not be visible on the page that you have not selected. At last click on the Save button.

Create a formula field using Now function in Salesforce

As we click on the Save button the custom formula field will be saved in the object selected above. In this example, we have created the formula field in the Leads object so we will open a record to check this field.

In the above image, we can see that the formula field Open Since Days shows the number of Days since the condition is true when the Lead status is Open.

In this way, we have successfully executed the NOW() function in Salesforce Classic with the help of the above steps by creating a custom formula field.

Conclusion

In this Salesforce tutorial, we have learned about the NOW() function in Salesforce, and with the help of the above steps we successfully created a custom formula field using the NOW() function in Salesforce Lightning and Salesforce Classic respectively.

Along with this we also learned about the NOW() function with its use cases and discussed the considerations that should be followed while using the NOW() function in Salesforce.

You may like to read: