Wednesday, January 25, 2017

Example of BusinessHours calculation


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 organization:
business-hour
The solution can be achieved upon using following function:

BusinessHours.add(businessHoursId, startDate, intervalMilliseconds)

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 to clarity I have also printed my local time which helps me in easy calculation.


Utility Class



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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);
    }
   
}

Usage:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
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

1
2
3
4
5
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 about Business Hours refer this: Apex Class BusinessHours

2 comments:

  1. Excellent Blog!! What you explained is absolutely correct, at present CRM helps to track your customer effectively. Your ideas to extend the functionality in CRM will definitely help more to use CRM effectively. I found the best ways to get leads to my business.
    Regards
    Salesforce Developer 501 Training in Chennai
    Salesforce Developer 502 Training in Chennai

    ReplyDelete
  2. Nice blog and absolutely outstanding. You can do something much better but i still say this perfect.Keep trying for the best...

    Embedded training in chennai | Embedded training centre in chennai
    PLC Training institute in chennai

    ReplyDelete