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.
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.
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.
2. On the Setup page of Salesforce Lightning, click on the Object Manager tab.
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.
5. From the list of Data types, select the Formula data type and click on the Next button.
6. In this step, enter the Field Label for this formula field. In this example, I have entered the field label as Event Hour, and as we click on Field Name, it will be auto-filled according to the entered Field Label.
In the Formula Return Type section, select the number type for the output. We are using the HOUR() function so the output will be a number. For example, If the time is 5 PM the output will be 17 according to 24 hours format.
Select the Number option and click on the Next button.
7. In this step, we will apply the Hour() formula field and for that click on the Functions dropdown and select the HOUR function.
8. After adding an HOUR() function, we need to add an expression in this function, and to do that click on the Insert Field button.
9. Select a time or date/time field from the list of fields in this case, I have selected a custom time field Event time apart from this there is also a standard date/time field like Created date.
We can only select a field that is either a date/time field or a time field for the HOUR() function.
After selecting a field click on the Insert button.
10. As we click on the Insert button, the selected field will be added as an expression in the HOUR() function. Here, click on the Check Syntax button to check the formula.
11. If the applied formula does not give an error then click on the Next button.
12. In this step, we will establish field-level security for this field and activate the checkboxes of the profiles to give them access to this field and then click on the Next button.
In this case, I have activated the Visible checkbox to make this field accessible to all user profiles.
13. In this window, make sure to activate the page layout checkbox to make this field visible to the users on the record page, and last click on the Save button.
As we click on the Save button, the formula field with the HOUR() function will be created in the selected object.
Now go to the object in which you have created this field and in this example, I have created this field in the Campaign object.
In the record of the Campaign, I can view this field Event Hour which is extracting hour from the Event time field.
In this way, we can use the HOUR() function through fields, reports, and other objects in Salesforce Lightning.
How to use the HOUR() function in Salesforce Classic
In the below steps, we will learn to utilize the HOUR() function using a formula field in Salesforce Classic.
1. Navigate to the Setup page of Salesforce Classic and for that click on the Setup tab that is on the upper right side of the window.
2. On the Setup page, go to the left sidebar and click on the Customize dropdown.
3. In this dropdown select the object in which you have to create a custom formula field using the Hour() function.
In this example, I have selected the Contacts objects. After this click on the Fields option of the object dropdown.
4. In the Fields setup of the object, scroll down to the section Custom Fields & Relationships and click on the New button.
5. In this step, select the Formula data type and click on the Next button.
6. In this step, we will define the output of the HOUR() function. First, name the output in the Field Label and it will auto-generate the Field Name. In this example, I have named the output field Contact Hour.
In Formula Return Type activate the Number radio button and click on the Next button.
7. Now we will enter the formula using the HOUR() function and for that go to the Functions scroll bar on the right-hand side select the HOUR function then click on the button Insert Selected function.
8. After adding the HOUR() function, click on the button Insert Field to add an expression for the HOUR function.
9. In this window, you will see a list of fields and from here select a Time or Date/Time field. If you choose any other field type, you will get an error.
In this example, I have selected a custom time field named Contact Activation.
10. After adding an expression to the HOUR() function click on the button Check Syntax to verify the formula and after successful verification click on the Next button.
11. In this step, select profiles to whom you have to make this field visible or you can activate the Visible checkbox to make this field visible to all users. After this click on the Next button.
12. Make sure that the page is activated, this will make the field visible on the record’s page layout. At last click on the Save button.
As we click on the Save button the custom formula field with the Hour function will be created in the selected object.
I have created this formula field in the Contacts object so that on the record page of any Contact I can see this Hour function field. This field will display the hours from the field that we used as an expression in the formula in the above steps.
In the above image, we can see that the Contact Hour field is displaying the hours from the Contact activation field. It extracts hours in the 24-hour format from the time field.
In this way, we can use the HOUR() function by creating a custom formula field in Salesforce Classic.
In this blog, we learned about extracting Hour from the datetime field in Salesforce Lightning and Salesforce Classic edition by creating a formula field with the help of the HOUR() function.
Along with this, we understood the uses of HOUR function and also discussed the considerations that we should be aware of while using the HOUR function in Salesforce.
You may like to read:
I am Bijay Kumar, the founder of SalesforceFAQs.com. Having over 10 years of experience working in salesforce technologies for clients across the world (Canada, Australia, United States, United Kingdom, New Zealand, etc.). I am a certified salesforce administrator and expert with experience in developing salesforce applications and projects. My goal is to make it easy for people to learn and use salesforce technologies by providing simple and easy-to-understand solutions. Check out the complete profile on About us.