Check date time occurs within Business Hours Salesforce Apex

Check date time occurs within Business Hours Salesforce Apex

In Salesforce Apex, we can make use of BusinessHours.isWithin() method to check whether the date time is within the business hours or not.

Please check the following apex code for reference.

Sample Apex Code:

Sample Code:

// Get the business hours
Id BHId = [
    SELECT Id 
    FROM BusinessHours 
    WHERE Name = 'Default' 
    LIMIT 1
].Id;

// Current date and time
Datetime now = System.now();
System.debug( 'now::' + now );

// Check if fall within business hours
Boolean isWithinBH = BusinessHours.isWithin(
    BHId, now
);
System.debug( isWithinBH );

Leave a Reply