How to add one hour to a DateTime field using Trigger in Salesforce?

How to add one hour to a DateTime field using Trigger in Salesforce?

addHours() can be used to add one hour to a Date Time field using Trigger in Salesforce.

Sample Trigger:

trigger AccountTrigger on Account( before insert ) {
    
    for ( Account objAcc : trigger.new ) {
    
        if ( objAcc.Start_Date_Time__c != null ) {
        
            objAcc.End_Date_Time__c = objAcc.Start_Date_Time__c.addHours( 1 );
        
        }
    
    }
    
}

Output:

Leave a Reply