Salesforce HOUR() Function

In this Salesforce tutorial, we will learn about the HOUR() function. In Salesforce CRM we create and update records in day-to-day tasks. While creating or modifying records we get the time of that activity.

In case, you want to extract the hour from this time then in that situation HOUR() function is going to help you. The HOUR() function is executable for Salesforce Lightning and Salesforce Classic editions.

What is HOUR function in Salesforce

In Salesforce, the HOUR() function is a Date/Time function used in formulas to extract the hour component from a given date-time or time value. The HOUR() function helps us in obtaining the hour portion of a date-time field.

The HOUR() function returns the local time hour value without the date in the form of a number from 1 through 24.

For example, if you created a record at 6:00 pm then the HOUR() function will return the output 18 according to 24-hour time format.

We can also parse the time as a text in the HOUR() function like HOUR(TIEVALUE (“18:30:00.125”) returns 18.

Considerations:

There are a few considerations that we should be aware of while using the HOUR() function in custom fields or reports in Salesforce.

  • The Hour() function is used with DateTime fields, make sure you are using a field with DateTime or Time data type otherwise, it will give an error.
  • The output of the Hour() function will be generated according to Coordinated Universal Time (UTC). If you are not getting the results according to your system time then consider adjusting time zones, and the results accordingly.

Syntax of the Salesforce HOUR function

The Salesforce Hour function is used with the help of the Syntax below.

Hour(time)

Here in the time we have to add an expression for the Hour function. We can replace time with a time value or value such as TIMENOW(), TIMEVALUE(), DATEVALUE(), DATETIMEVALUE(), and any other custom date/time field.

How to use the HOUR function in Salesforce Lightning

Now we will discuss the step-by-step implementation of HOUR Function to the fields in Salesforce Lightning.

1. Navigate to the Setup page of Salesforce Lightning and to do that click on the settings icon and from the options in the dropdown select the option Setup.

Hour function use cases in Salesforce

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

Hour Function formula field in Salesforce Lightning

3. In the Object Manager select the object in which you have to apply the HOUR function and for this, I have selected the object Campaign.

4. In the setup of the selected object, click on Fields and Relationships on the left-hand sidebar. In the Fields and Relationships window, click on the New button.

Create a Field using Hour Function in Salesforce Lightning

5. From the list of Data types, select the Formula data type and click on the Next button.