Salesforce TIMENOW Function

In this Salesforce tutorial, we will discuss the TIMENOW function in Salesforce. This function will be helpful for extracting the current time from the time field. The TIMENOW function is executable in Salesforce Lightning and Salesforce Classic editions.

What is TIMENOW() function in Salesforce

In Salesforce, the TIMENOW() function returns a time value in GMT that represents the current moment or current time. We can use the TIMENOW function instead of the NOW function to track time, without a date.

Syntax of TIMENOW() function

The syntax of this function is written in the following way:

TIMENOW()

Here, we do not need to enter any value in the parenthesis of the function.

Considerations of TIMENOW in Salesforce

There are some considerations to use the TIMENOW() function in Salesforce.

  • Do not remove the parentheses in the formula, validation rule, or place where you execute this function.
  • Keep the parentheses empty. They do not need to contain a value.
  • Use the TODAY function and parse the TIMENOW function in that if you prefer to use a date field.
  • The displayed value is based on the timezone settings on your system.
  • Select the output type as time, if you are not using this function with other functions.

How to use TIMENOW() Function in Salesforce Lightning

In the below steps, we will discuss the execution of the TIMENOW function in Salesforce Lightning with the help of a custom formula field. Now login to your developer account and follow the below steps.

In this example, we have defined the TIMENOW function with a formula where we will get the time with added hours. To understand this we can assume that we have a task that will take approximately 5 hours to complete so we can add those 5 hours to the TIMENOW() function with the help of the “+” operator and we will get the estimated time of task completion in output.

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

TIMENOW function in Salesforce Lightning

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

Salesforce Lightning TIMENOW function

3. In the Object Manager setup select the object in which you have to create a formula field using the TIMENOW() function and in this example, I have selected the object Opportunity.

Salesforce Lightning TIMENOW function use case

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

Create a formula field with TIMENOW function in Salesforce

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

Timenow function use case in Salesforce Lightning

6. In this step, we will define the output of the formula field.

Here, enter the output label in the Field Label. In this example, I have entered the label as Expected Completion Time. The Field Name will be auto-filled according to the entered Field Label.

After this select the Formula Return Type as Time and click on the Next button.

Timenow function output type in Salesforce Lightning

7. In this window we will enter a formula in the formula editor using the TIMENOW() function and for that go to the Functions scroll bar on the right side and select the TIMEVALUE() function then click on the button Insert Selected Function.

This TIMEVALUE() function will return time in AM, PM format

Salesforce Lightning TIMENOW function use case

8. After adding the text function the formula will be TIMEVALUE(Value). Here, replace the Value with the TIMENOW() function that we will again select from the Functions section.

9. In this formula, I needed the time 5 hrs ahead of UTC time so I added those hours to the current time i.e. Timenow() to get the Time value of hours of UTC.

The formula for this output is as follows:

TIMEVALUE(TIMENOW() + 5/24)

10. Now click on the button Check Syntax to validate the formula and if there is no error in the formula click on the Next button.

Timenow function Syntax in Salesforce Lightning

11. In this step, we have to establish the field level of security for the formula field and for that select the profiles to which you have to give access to this field.

In case, you have to give access to all users then activate the Visible checkbox and click on the Next button.

Salesforce Lightning TIMENOW function Formula field

12. Activate the page layouts on which you have to activate this formula field. This field will not be visible on the page layout that you don’t activate.

After selecting the page layouts, click on the Save button.

Save the Salesforce formula field with Timenow function in lightning

As we click on the Save button the formula field will be saved in the selected object. When you open the record of the object in which you have created the formula field it will give you the current time in that field.

In this case, the current time was 11:48 and according to the applied formula, the output is 5 hours ahead of the current time.

Salesforce Lightning Custom formula field with Timenow function

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

How to use TIMENOW() function in Salesforce Classic

In this example, we will use the TIMENOW() function to track the Status of the Campaigns that the campaign is either active or inactive.

We will execute the TIMENOW function with a conditional statement, where the condition will be True the output will be Active when the TIMENOW() (current time) is between Start Time and End Time otherwise, the output will be Inactive.

In the below steps, we will create a Custom formula field using the TIMENOW() function.

1. On the Home page of Salesforce Classic, click on the Setup tab that is on the upper right side of the screen.

TIMENOW function in Salesforce Classic

2. On the Setup page of Classic, go to the Build section on the left-hand sidebar and select the object then click on the Fields in the dropdown of object.

In this example, I have selected the object Campaigns.

Timenow function use case in Salesforce Classic

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

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

Salesforce Classic formula field with Timenow function

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

Enter a relevant name for the output in the Field Label and the Field Name will be auto-filled according to the entered field label.

Select the formula output type as Text and click on the Next button.

Salesforce Custom formula field with Timenow function in Classic

6. In the formula editor, we will define a condition using the TIMENOW() function that will return the campaign status. The formula will be as follows:

IF(
    AND(
        TIMENOW() >=  Start_Time__c ,
        TIMENOW() <  End_Time__c 
    ),
    "Active",
    "Inactive"
)

In the above formula we have used the following fields and functions:

  • The TIMENOW() function returns the current date and time.
  • The AND() function is used to combine multiple conditions. In this case, it’s checking two conditions:

(a) TIMENOW() >= Start_Time__c , This condition checks if the current time is greater than or equal to the value stored in the field Start Time.

(b) TIMENOW() < End_Time__c, This condition checks if the current time is less than the value stored in the field End Time.

  • The IF() function is used to evaluate the combined conditions in the AND() function. If both conditions are true (i.e., the current time is within the specified time range), the formula returns “Active.” Otherwise, it returns “Inactive.”

7. The TIMENOW() function in the above formula you can enter manually or go to the Functions scroll bar select the TIMENOW() function, then click on the button Insert the Field.

Salesforce Classic Create a Formula field with Timenow function

8. The custom time fields Start Time and End Time can be inserted using the button Insert Field that is above the formula editor.

9. Now click on the button Check Syntax to validate the formula and on successful validation click on the Next button.

Salesforce Classic Timenow function execution in formula field

10. In this step, we have to select profiles to give access to this field. The unselected fields won’t have access to this field.

Activate the Visible checkbox if you want to make this field visible to all users and click on the Next button.

Field Level Security for Timenow Formula field in Salesforce

11. In this window, activate the Page Layouts to make this field visible on the Page layouts. This field will be visible only in the selected page layouts.

After selecting the page layouts, click on the Save button.

Save the Salesforce formula field with Timenow function

As we click on the Save button the formula field will saved in the selected object.

Now to check the output of the formula field that we created using the TIMENOW() function, go to the object and open a record.

TIMENOW function formula output in Salesforce

In the above image, we can see the created formula field Campaign Status is showing the status as Inactive according to the condition applied in the formula.

We applied the condition that the output will be Inactive when we open the record and the time is less than the Start Time or more than the End Time.

The current time or TIMENOW() 12:35 is less than the Start and End Time, hence we get Inactive as output.

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

Conclusion

In this Salesforce tutorial, we have learned about the TIMENOW() function. We discussed a use case of the TIMENOW function by creating a custom formula field using the TIMENOW function in Salesforce Lightning and Salesforce Classic respectively.

Along with this we also learned about syntax and the considerations that we should be aware of while using the TIMENOW function in Salesforce.

You may like to read: