Illustration of Business Hours Computation

We have a need to dispatch an email alert to users following a span of 4 business hours from the Task Creation Date. Our designated business hours span from 7 am to 5 pm, Monday through Friday. For instance, if a Task is initiated at 4 pm on a Monday, we aim to send the email at 10 am on the following Tuesday. This accounts for the single remaining business hour on Monday and the subsequent three hours during the next business day.

Organization’s Business Hours:

You can attain the solution by employing the following function:

BusinessHours.add(businessHoursId, startDate, intervalMilliseconds)

I have configured my user’s timezone as PST, in accordance with your Business Hours settings, and have implemented the following code. I’ve set SLAHours to 6 to ensure it extends beyond the current day’s business hours. It’s worth noting that System.now() returns the time in UTC, so for the sake of clarity, I’ve included the printout of my local time, which aids in simplified calculations.

Helper Class

public with sharing class BusinessHoursServices
{
    //retrieves & assign the Organization level Business Hours to defaultBH 
    @testVisible static BusinessHours defaultBH
    {
        get
        {
            if (defaultBH == null)
                defaultBH = [SELECT Id FROM BusinessHours WHERE IsDefault = true];
            return defaultBH;
        }
        private set;
    }
        
    public static Boolean isWithin()
    {
        return isWithin(Datetime.now());
    }
    
    public static Boolean isWithin(Datetime input)
    {        
        return BusinessHours.isWithin(defaultBH.Id, input);
    }

    public static  Datetime nextStartDate()
    {
        return nextStartDate(Datetime.now());
    }
  
    public static Datetime nextStartDate(Datetime input)
    {
  return BusinessHours.nextStartDate(defaultBH.Id, input);
    }
   
    public static Datetime getSLATimeByBusinessHour (DateTime input, Integer SLAhours)
    {        
        return BusinessHours.add(defaultBH.Id,input, SLAhours* 60 * 60 * 1000L);
    }
   
}

Utilization:

Datetime currentTime = System.now();
System.debug('current time=' + System.now());
TimeZone tz = UserInfo.getTimeZone();
System.debug('Display name: ' + tz.getDisplayName());

System.debug('Current local time=' + currentTime.format('MM-dd-yyyy ') + ' ' 
  + currentTime.format('h:mm a'));

Id businessHourId = [SELECT Id FROM BusinessHours WHERE IsDefault = true].id;

//check the current datetime falling on the same business day
Boolean isSameDayWithinBusinessHour =  BusinessHoursServices.isWithin(System.now());
System.debug('isSameDayWithinBusinessHour=' + isSameDayWithinBusinessHour);

Integer slaHours = 6;
Datetime targetDT = BusinessHours.add(businessHourId,currentTime, SLAhours* 60 * 60 * 1000L);
System.debug('target date on local time =' + targetDT.format('MM-dd-yyyy ') + ' ' 
             + targetDT.format('h:mm a'));

Debugging Log

USER_DEBUG|[2]|DEBUG|current time=2016-11-09 20:29:17
USER_DEBUG|[4]|DEBUG|Display name: Pacific Standard Time
USER_DEBUG|[6]|DEBUG|Current local time=11-09-2016  12:29 PM
USER_DEBUG|[13]|DEBUG|isSameDayWithinBusinessHour=true
USER_DEBUG|[16]|DEBUG|target date on local time =11-10-2016  8:29 AM