3

We have a requirement to send an email alert to users after 4 business hours from Task Creation Date ie., Our Business hours are from 7am to 5pm (Monday to Friday) and if Task is created at 4pm on Monday, we need to send an email at 10am on Tuesday. Because only 1 business hour was left on Monday and remaining 3 hours on next business day.

Business hours in our Org: enter image description here

We have created a custom Date/Time field on Task Object named Task_Closure_Date_Time__c and trying to populate it's value by adding 4 business hours to Task Created Date as shown below.

Logic in Task trigger handler class:

public with sharing class TaskTriggerHandler
{
for(Task newObjTask:[Select ID,CreatedDate,Task_Closure_Date_Time__c  From Task Where Id IN:taskIdMap.keySet()])
                {
                    newObjTask.Task_Closure_Date_Time__c = BusinessHoursServices.getSLATimeByBusinessHourGmt(newObjTask.CreatedDate,4);
                    updatedTaskList.add(newObjTask);    
                }
}

BusinessHoursServices class:

public with sharing class BusinessHoursServices
{
    static BusinessHours defaultBH
    {
        get
        {
            if (defaultBH == null)
                defaultBH = [SELECT Id FROM BusinessHours WHERE IsDefault = true];
            return defaultBH;
        }
        private set;
    }
 public static Datetime getSLATimeByBusinessHourGmt (DateTime input, Integer SLAhours)
    {
        return date.valueof(BusinessHours.addGmt(defaultBH.Id,input, SLAhours* 60 * 60 * 1000L));
    }
}

Now the returned Task_Closure_Date_Time__c value is falling on same day i.e., on "Created Date" irrespective of SLAhours passed. It is not considering the next business day. (hours part of Time is getting changed but Date remains the same)

We would like to use this Custom Date/time field in process builder to send an email alert.

Please let me know how to calculate the correct Date/Time value by taking business hours into consideration.

2
  • 1
    Should you perhaps be using plain old add and ignore GMT in this case?
    – Adrian Larson
    Commented Nov 9, 2016 at 17:53
  • @AdrianLarson your guess is correct Commented Nov 9, 2016 at 20:53

1 Answer 1

3

The issue will be resolved if you use

BusinessHours.add(businessHoursId, startDate, intervalMilliseconds)

which @Adrian has mentioned in comments. Find my analysis:

I have configured my user's timezone PST as per your Business Hours screen and establish following code. I have mentioned SLAHours as 6 so that it could past today's business hours.

Note that, system.now() returns UTC time, so for sake of clarity I have also printed my local time which helps me in easy calculation.

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'));

Debug 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

Understanding of calculations

Business hours = 7AM to 5PM PST It will take (5PM - 12:29PM) i.e. 4 hours 31 mins on today and rest 1 hour 29 mins on next day starting at 7AM.

Finally SLA end time is 11-10-2016 8:29 AM.

For more information, refer BusinessHours class

1
  • Thank you Santanu and Adrian Larson for providing the resolution.
    – Ravi
    Commented Nov 10, 2016 at 2:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .