Add Hour to DateTime Field in Salesforce

In this Salesforce Tutorial, we will learn how to Add Hour to DateTime Field in Salesforce. Moreover, we will go through the steps using which we can Add Hour to DateTime Field in Salesforce Lightning and Salesforce Classic.

In Salesforce, there are many scenarios where we need to Add Hour to DateTime in Salesforce, such as scheduling a task, calculating the end or start time of a campaign, managing business meeting hours, etc.

And, In all these cases, we need to Add the Hour to DateTime field in Salesforce. So, If you want to explore How to Add an Hour to DateTime Field in Salesforce Lightning and Classic Edition, you have come to the right place.

The following are the topics that we will cover in this Salesforce Tutorial:

  • Salesforce Date/Time Field
  • Salesforce AddHours Function
  • Add Hour to DateTime Field in Salesforce Lightning using Triggers
  • Add Hour to DateTime Field in Salesforce Classic using Triggers

Salesforce Date/Time Field

In Salesforce, the Data Type is used to represent which type of data we can store in the field of an object. And Date/Time is one of the standard data types in Salesforce.

It is used to store both the date and time of the record in a field of the object. And to store data in the Date/Time Field, we use the standard date and time format.

The following Format of the Salesforce Date/Time Field is as given below, we can use any one of them:

. YYYY-MM-DD
. YYYY-MM-DD hh:mm:ss
. YYYY-MM-DDThh:mm:ssZ
. YYYY-MM-DDThh:mm:ss.sssZ
  • YYYY represents the year in four digits.
  • MM represents the month in two digits.
  • DD represents the day in two digits.
  • T represents a separator that indicates the start of the time period.
  • hh represents the hour in 24 formats in two digits.
  • mm represents the minute in two digits.
  • ss represents the second in two digits.
  • sss represents the milliseconds in three digits and it is an optional parameter.
  • Z represents the time zone in UTC form.

For example, the DateTime Field is “2023-03-25T13:28:02.000Z” which means March 23rd, 2023 at 1:28 PM UTC.

With, this we have learned about Salesforce Date/Time Field. Now, we will move ahead and learn about the Salesforce AddHours function.

Salesforce AddHours() Function

In Salesforce, to add a specific number of hours in a Date/Time value, we use the ADDHOURS() function. The syntax of the ADDHOURS() function is as given below:

ADDHOURS(Date/Time,hours)

The parameters are defined below:

  • Date/Time: It represents the date and time value, to whom we want to add hours.
  • Hours: It represents the number of hours we want to add to the DateTime value. The value can be positive or negative as per need.

Let’s understand it in detail with the help of an example:

Suppose, we want to create a formula that adds 3 hours to the start date and time field to calculate the end date and time of the meeting.

Here, is the formula:

ADDHOURS(Start_Time_c, 3)

With, this we have learned about the Salesforce AddHours function. Now, we will move further and see How to Add Hour to DateTime field in Salesforce Lightning.

Add Hour to DateTime Field using Triggers in Salesforce Lightning

Here are the steps to Add the Hour to Date/Time field using a trigger in Salesforce Lightning.

  • Log in to your Salesforce account and navigate to Setup.
  • On the left-hand menu, click on Object Manager.
Add Hour to DateTime Field using Triggers in Salesforce Lightning
Add Hour to DateTime Field using Triggers in Salesforce Lightning
  • Click on the object for which we want to add an hour to the DateTime field. Here, I select the Lead object from the list of objects.
Add Hour to DateTime Field using Triggers in Salesforce Lightning Example
Add Hour to DateTime Field using Triggers in Salesforce Lightning Example
  • In the left-hand menu of the object page, click on the Triggers.
How to Add Hour to DateTime Field using Triggers in Salesforce Lightning
Add Hour to DateTime Field using Triggers
  • Click on the New button to create a new trigger to add an hour to the DateTime field.
How to Add Hour to DateTime Field using Triggers in Salesforce Lightning Example
Create Trigger in Salesforce to Add Hour
  • Create the Apex Trigger to add three hours to the End_Date_Time__c field in Salesforce Lead Object. Past the below-given code:
trigger LeadTrigger on Lead( before insert ) {
    
    for ( Lead objLead : trigger.new ) {
    
        if ( objLead.Start_Date_Time__c != null ) {
        
            objLead.End_Date_Time__c = objLead.Start_Date_Time__c.addHours( 3 );
        
        }
    
    }
    
}

In the Salesforce Apex code, we create a trigger that executes before an event occurs on the lead record. It automatically adds the three hours to the End_Date_Time__c field of the Salesforce Lead Object.

Here, we create a trigger with the unique name ‘LeadTrigger’ which is associated with the ‘Lead’ object, and event the trigger ‘before insert’.

It adds the 3 hours to the custom field called ‘End_Date_Time__c’ with respect to the ‘Start_Date_Time__c’ custom field using the addHours function.

  • To save the trigger, click on the Save button.
Add Hour to DateTime Field using Triggers in Salesforce
Add Hour to DateTime Field using Triggers in Salesforce
  • Once the trigger is created, open the Lead object and create a new lead with the Start Date Time value and save it. After that, click on the Details section and here we will get the End Date Time field with a value having 3 more hours.
Add Hour to DateTime Field using Triggers in Salesforce Example
Add Hour to DateTime Field using Triggers

With this, we have learned to add the Hour to DateTime field using Trigger in Salesforce Lightning. Next, we will see how we can add an Hour to DateTime field using Trigger in Salesforce Classic.

Read Create Custom Picklist Field in Salesforce

Add Hour to DateTime Field using Triggers in Salesforce Classic

Here are the steps to Add the Hour to Date/Time field using a trigger in Salesforce Classic.

  • Click on Avtar and then select Switch to Salesforce Classic to log in to our Salesforce Classic account.
Add Hour to DateTime Field using Triggers in Salesforce Classic
Add Hour to DateTime Field using Triggers in Salesforce Classic
  • Go to the Setup menu in Salesforce Classic.
Add Hour to DateTime Field using Triggers in Salesforce Classic Example
Add Hour to DateTime Field using Triggers in Salesforce Classic
  • Click on the Build section.
  • Then, under Customize section select the Object for which we want to add an hour to the DateTime field. Here, I select the Price Books object from the list of objects.
  • Click on the dropdown arrow and select Triggers.
How to Add Hour to DateTime Field using Triggers in Salesforce Classic
Add Hour to DateTime Field using Triggers
  • To create a new trigger to add an hour to the DateTime field, click on the New button.
How to Add Hour to DateTime Field using Triggers in Salesforce Classic Example
Create New Trigger to Add Hour to DateTime
  • Create the Apex Trigger to add 1 hour to the End_Offer_Date_Time__c field in Salesforce Price Books object. Past the below-given code:
trigger PriceBookTrigger on Pricebook2( before insert ) {
    
    for ( Pricebook2 objPricebook : trigger.new ) {
    
        if ( objPricebook.Start_Offer_Date_Time__c != null ) {
        
            objPricebook.End_Offer_Date_Time__c = objPricebook.Start_Offer_Date_Time__c.addHours( 1 );
        
        }
    
    }
    
}

In the Salesforce Apex code, we create a trigger that executes before an event occurs on the price book record. It automatically adds the one hour to the End_Offer_Date_Time__c field of the Salesforce Price Books Object.

Here, we create a trigger with the unique name ‘PriceBookTrigger’ which is associated with the ‘Price Books’ object, and event the trigger ‘before insert’.

It adds the 1 hour to the custom field called ‘End_Offer_Date_Time__c’ with respect to the ‘Start_Offer_ Date_Time__c’ custom field using the addHours function.

  • To save the trigger, click on the Save button.
Salesforce Classic Add Hour to DateTime Field using Triggers
Salesforce Classic Add Hour to DateTime Field using Triggers
  • Once the trigger is created, open the Price Books object and choose any price book record. We will get the End Offer Date Time field with a value having one more hour.
Salesforce Classic Example Add Hour to DateTime Field using Triggers
Add Hour to DateTime Field using Triggers

With this, we have learned to add the Hour to DateTime field using Trigger in Salesforce Classic.

Conclusion

We have learned about Salesforce DateTime Fied. In addition, we have also explored the Salesforce ADDHOURS function which is used to add or minus certain hours from the DateTime field.

Moreover, we have learned that both Salesforce Classic and Salesforce Lightning provide a simple procedure to Add Hours to DateTime Field.

In addition to this, the following are the topics that we have discussed:

  • Salesforce Date/Time Field
  • Salesforce AddHours Function
  • Add Hour to DateTime Field in Salesforce Lightning using Triggers
  • Add Hour to DateTime Field in Salesforce Classic using Triggers

You may also like: