0

My use case is to calculate the deadline to respond on the Case. Rule is to add 10 business days from the complaint received date.

  1. Defined the business hours: enter image description here

  2. Logic 1

    Date initialDate = Complaint_Receive__c;
    
    Datetime datetime = datetime.newInstance(initialDate.year(), 
                initialDate.month(),initialDate.day());
    Datetime deadline = BusinessHours.addGmt (stdBusinessHours.id, datetime, 
                 10 * 24 * 60 * 60 * 1000l);
    
    Datetime casedeadline = date.newinstance(deadline.year(), 
                            deadline.month(), deadline.day());
    

If the Complaint Received date is 04/01 then the deadline should be 04/15. But the above logic is returning 04/13 which is Saturday.

But if I change the logic to the below it is returning the deadline as 04/15 which is accurate.

Logic 2

Date initialDate = Complaint_Receive__c;

Date initialDatePlus = Complaint_Receive__c.addDays(1);
Datetime datetime = datetime.newInstance(initialDate.year(), 
initialDate.month(),initialDate.day());
Datetime deadline = BusinessHours.addGmt (stdBusinessHours.id, datetime, 10 * 23 * 60 * 60 * 1000l);

The only difference is adding one day to the complaint received date and used 23 hours in addGmt method instead of 24. To me Salesforce should return the deadline as 04/15 for Logic 1 itself. Please explain what I am missing here.

Note - Interestingly the Logic 1 is working for some of the date but not all.

2
  • 1
    Did you account for Daylight Savings Time?
    – sfdcfox
    Apr 24, 2019 at 21:33
  • No. I haven't. apart from the above business hours configuration and apex code. I haven't done any additional configuration for daylight savings. Apr 25, 2019 at 14:26

1 Answer 1

1

The timezone in BusinessHours has been assigned as EDT.

So, in your calculation, use BusinessHours.add() which returns the result Datetime in the local time zone.

Datetime deadline = BusinessHours.add(stdBusinessHours.id, datetime, 
             10 * 24 * 60 * 60 * 1000L);

In case of BusinessHours.addGmt(), it returns the result Datetime in GMT.

Thats why it is showing one day off.

5
  • Correct me if I am wrong. Salesforce (i.e addGMT) shows the date value in the UI as per the users timezone. Also if the complaint received date is 04/01 then why the result is 04/13 which is Saturday (non- business day). Apr 25, 2019 at 14:23
  • Are you querying correct BusinessHours? I can see in the picture that it is not default BusinessHour Apr 25, 2019 at 14:45
  • I am querying the correct business hours. Here is my query, BusinessHours stdBusinessHours = [select id from businesshours where isDefault = false AND Name='BusinessDays' limit 1]; There is one more business hours(default) entry for Calendar days(Includes Saturday & Sunday) Apr 25, 2019 at 15:05
  • Since you are calculating the date from Apex and showing on the screen, it will not show based on user's timezone. so addGMT() will show date on GMT, not the user's timezone. If datefield is saved as part of Salesforce record then during display, it will show based on user's timezone as defined in User record. Hope it makes sense! Thats why use add() method to show in user's timezone. Apr 25, 2019 at 15:12
  • It is same result even if I use the BusinessHours.add(). Case cs = new Case(Id='xxxx'); Date dt = Date.newInstance(2019, 4, 1); Datetime startdatetime = datetime.newInstance(dt.year(), dt.month(),dt.day()); System.debug('Date Time****'+startdatetime); BusinessHours stdBusinessHours = [select id from businesshours where isDefault = false AND Name='BusinessDays' limit 1]; Datetime resultdatetime = BusinessHours.add(stdBusinessHours.id, startdatetime, 10 * 24 * 60 * 60 * 1000L); System.debug('resultdatetime****'+resultdatetime); Apr 25, 2019 at 19:10

You must log in to answer this question.

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