How to get the current hour in the local time zone of the context user using apex in Salesforce?

How to get the current hour in the local time zone of the context user using apex in Salesforce?

hour() returns the hour component of a Datetime in the local time zone of the context user.

Sample Code:

trigger CaseTrigger on Case (before insert) {
    
    Boolean tomorrowTargetBool = system.now().hour() >= 12 ? true : false;
    for ( Case c : trigger.new ) {
        if ( tomorrowTargetBool ) 
            c.Target_Date__c = system.today() + 1;
    }
    
}

In the above example, if the case is created after 12 PM, the target date field will be next day.

Leave a Reply