Salesforce TIMEVALUE Function

In this Salesforce tutorial, we will learn about the TIMEVALUE function in Salesforce. We will discuss the use case of this function in Salesforce Lightning and Salesforce Classic Edition.

This function comes in use when you have to extract the time from the DateTime field.

What is TIMEVALUE() function in Salesforce

In Salesforce TIMEVALUE() function returns the time value without the date, such as business hours and other time fields. With the help of this function, we can extract the time from any DateTime/Time field.

TIMEVALUE() function Syntax

The syntax for the TIMEVALUE function is written in the following way.

TIMEVALUE(expression)

In the above syntax, the expression or the value is supposed to be replaced by the datetime field.

For example, if we use the expression as text like TIMEVALUE(“15:30:10.200”) it will return 3:30 pm in the output.

In Lightning, it returns seconds also but in Classic edition it returns only hour and minute in the output.

Considerations

There are a few considerations that we should be aware of while using the TIMEVALUE() function in Salesforce.

  • The displayed value of the output will be based on the timezone settings of your org.
  • Do not use the Time field as an expression for the TIMEVALUE() function because a  time field’s value is already in time format.
  • Use only the DateTime/Time field as a value for expression in the TIMEVALUE() function.

How to use the Salesforce TIMEVALUE() function in Salesforce Lightning

In this example, we will create a custom formula field using the TIMEVALUE() function. With the help of this formula field, we will track the time of day of the meeting i.e. the formula displays whether the meeting was held in the morning or afternoon.

In the below steps, we will discuss the execution of TIMEVALUE() function in Salesforce Lightning with the help of a custom formula field in an object.

1. Navigate to the Setup page of Salesforce Lightning and for that click on the settings icon that is on the upper right side of the window and from the options select Setup.

TIMEVALUE function in Salesforce Lightning

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

Salesforce Lightning Timevalue function

3. In the object manager window select an object to create a custom formula field. In this example, I have selected the object Account.

Use Timevalue function in Salesforce Lightning

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

Create a custom field using Timevalue function

5. To create the formula field, select the data type as Formula and click on the Next button.

TIMEVALUE function execution in Salesforce Lightning

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

Enter the output field name in the Field Label and the Field Name will be auto-filled according to the entered Field Label. In this example, I have labeled the field as Time of Day.

In the Formula Return Type, select the option Text and click on the Next button.

Salesforce Lightning Timevalue function output field

7. Enter the below formula in the formula editor.

IF(VALUE(LEFT(TEXT(TIMEVALUE(  Meeting_time__c  )), 2)) < 12, 'Morning', 'Afternoon')

We have defined the above formula in the following way.

  • TIMEVALUE( Meeting_time__c) converts the value in the ” Meeting_time__c ” field to a time value.
  • The TEXT function converts the time value to a text representation.
  • (LEFT(TEXT(TIMEVALUE( Meeting_time__c )), 2) extracts the first two characters (hours) from the text representation of the time value.
  • The Value function converts the extracted text (hours) back to a numerical value.
  • The IF condition checks if the numerical value (hours) is less than 12. If the condition is True, it returns ‘Morning‘ else it will return ‘Afternoon‘.

8. After adding the above formula in the formula editor click on the Check Syntax button to validate the formula and on successful validation click on the Next button.

Validate the Timevalue function formula in Salesforce

9. In this step, we will establish the field-level security for the field and for that select the profiles to give them access to this field.

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

Salesforce Lightning Timevalue function use case

10. Activate the page layouts to make this field visible in the page layout. This field will be visible only in selected page layouts. At last click on the Save button.

Salesforce Lightning Timevalue function formula field

As we click on the Save button, the formula field will be saved in the object that we have selected in the steps above.

Now to check the output of the formula field that we have created using the TIMEVALUE () function, open the record of the object in which you have created this formula field.

In this example, I have created this formula field in the Account object, and in the record of the account, we can see the output in the field Time of Day as Morning when the time is 10:30 i.e. less than 12.

If time was more than 12 then output would be Afternoon according to the applied formula.

In this way, we have successfully executed the TIMEVALUE() function in Salesforce Lightning with the help of the above steps.

How to use TIMEVALUE() function in Salesforce Classic

In this example, we will use the TIMEVALUE() function to figure out whether the Case is modified or handled during Business hours or Extra Hours.

We will create a custom formula field where the output will be Business Hours if the time value is between 9 AM to 5 PM, if this condition is not true then the output will be Extra Hours.

To execute the TIMEVALUE() function in Salesforce Classic, follow the below steps.

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

2. On the Setup page go to the Build section click on the Customize dropdown and select the object. In the Object click on the option Fields. In this case, I have selected the “Cases” object.

Salesforce Classic Formula field with Timevalue function

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

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

Use Timevalue function in Salesforce Classic

5. In this step we will define the output of the formula field. Enter a relevant name for the output in the Field Label. In this example, I have entered the label as Case Hours.

Field Name will be auto-filled according to the entered Field Label. Select the Formula Return Type as Text and click on the Next button.

TIMEVALUE function output in Salesforce Classic

6. In this window, we will define the formula in the formula editor using the TIMEVALUE() function. The formula that we will use in this formula field is as follows.

IF(
  AND(
    VALUE(LEFT(TEXT(TIMEVALUE( LastModifiedDate )), 2)) < 9,
    VALUE(LEFT(TEXT(TIMEVALUE( LastModifiedDate )), 2)) > 18
  ),
  'Extra Hours',
  'Business Hours'
  )

We have defined the above formula in the following way.

  • The TIMEVALUE(LastModifiedDate) extracts the time component from the LastModifiedDate field and the result will be a time value.
  • The TEXT function converts the time value obtained into text format.
  • The LEFT function takes the first two characters from the text representation of the time and it extracts the hour part of the time.
  • The VALUE function converts the extracted hour from the text back to a numerical value.
  • The AND function checks if both conditions in the formula are true.
  • IF function checks if both conditions in the AND function are true, it returns the output ‘Extra Hours‘ else output will be ‘Business Hours‘.

7. Now click on the button Check Syntax and after successful validation click on the Next button.

Salesforce Classic Timevalue function execution

8. In this step, to set up the field-level security, 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.

Salesforce Classic Timevalue function Use case

9. 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.

TIMEVALUE function in Salesforce Classic

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 TIMEVALUE() function, go to the object and open a record.

TIMEVALUE function formula output in Salesforce Classic

In the above image, we can see the formula field that we created using the TIMEEVALUE() function is showing the output according to an applied condition in the formula as Business Hours because the time of modification is less than 6 pm.

Conclusion

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

Along with this we also learned about the use case, syntax, and the considerations that we should be aware of while using the TIMEVALUE() function in Salesforce.

You may like to read: