3

I will start off with a code snippet:

trigger CaseTrigger on Case (before insert, before update)
{

       BusinessHours defaultHours = [select Id from BusinessHours where IsDefault=true];

        if (Trigger.isUpdate && Trigger.isBefore)
        {
            for (Case updatedCase : Trigger.new)
            {
                System.debug('CreatedDate: ' + updatedCase.CreatedDate);
                System.debug('System.now: ' + System.now());
                System.debug('Case CreatedDate BusinessHours.diff: ' + BusinessHours.diff(defaultHours.Id, updatedCase.CreatedDate, System.now()));
                System.debug('Control BusinessHours.diff: ' + BusinessHours.diff(defaultHours.Id, Datetime.now().addDays(-1), System.now()));

                // ... other logic
            }
        }
}

And the Logs:

14:02:43:077 USER_DEBUG [70]|DEBUG|CreatedDate: 2018-08-09 08:27:42
14:02:43:077 USER_DEBUG [71]|DEBUG|System.now: 2018-08-09 11:02:43
14:02:43:077 USER_DEBUG [72]|DEBUG|Case CreatedDate BusinessHours.diff: 0
14:02:43:078 USER_DEBUG [73]|DEBUG|Control BusinessHours.diff: 36000000

Question:

Why BusinessHours.diff(defaultHours.Id, updatedCase.CreatedDate, System.now()) returns 0? I suspect the updatedCase.CreatedDate causes such a behavior, but I couldn't find any logical explanation for it.

Update 1:

Using Trigger.Old (I used Trigger.oldMap) didn't work:

System.debug('Case CreatedDate BusinessHours.diff: ' + BusinessHours.diff(defaultHours.Id, Trigger.oldMap.get(updatedCase.Id).CreatedDate, System.now()));

Debug log:

14:52:38:071 USER_DEBUG [72]|DEBUG|Case CreatedDate BusinessHours.diff: 0

Update 2:

As a workaround (suggested in the comments) it's possible to use a custom formula field that simply references the record's CreatedDate.

Created_Date_Custom_Formula__c

CreatedDate

Trigger:

System.debug('Case Created Date Formula BusinessHours.diff: ' + BusinessHours.diff(defaultHours.Id, updatedCase.Created_Date_Custom_Formula__c, System.now()));

Debug log:

15:11:18:055 USER_DEBUG [75]|DEBUG|Case Created Date Formula BusinessHours.diff: 276690

Update 3:

One key part that was missing in the initial question was the Business Hours table:

enter image description here

Note: Timezone for Business Hours is (GMT-05:00) Central Daylight Time (America/Chicago), whereas my timezone is (GMT+02:00)

6
  • What if you take older case? CreatedDate is your timezone, and System.now -- GMT, issue may be in that
    – kurunve
    Aug 9, 2018 at 11:21
  • Can you store createdDate in custom datetime field and check the difference with System.now() Aug 9, 2018 at 11:27
  • @kurunve, all calculations with dates happen in GMT. Internally CreatedDate is stored in GTM. The timezone comes into play when user sees the record in UI, for example.
    – Eduard
    Aug 9, 2018 at 11:58
  • @SantanuBoral, seems that's a workaround for now. But it's so inconvenient and I have to add some code overhead to make sure this field is always populated. I "love" apex.
    – Eduard
    Aug 9, 2018 at 12:09
  • 1
    Turned out that I can even use a custom formula field to reference the CreatedDate standard field. It's much nicer than a simple custom field. @SantanuBoral, thanks for the original idea.
    – Eduard
    Aug 9, 2018 at 12:13

2 Answers 2

1

Putting comments to the answer, since this approach has solved your purpose.

You can store the createdDate value in the custom field or use formula field to get CreatedDate value (@Eduard's comments) and use that field in the calculation.

2
  • Have to unmark your answer as accepted because the root case was in the Business Hours start time.
    – Eduard
    Aug 9, 2018 at 14:01
  • 1
    no issues, ideally when you test business hour related issues always make user's timezone and Business Hour's timezone as same for testing. Otherwise you make get confused with the results Aug 9, 2018 at 14:06
0

It appears to be a trivial thing - I tested the diff() between two dates when the Business Day for (GMT-05:00) Central Daylight Time (America/Chicago) timezone had not started. My timezone is (GMT+03:00) Kiev.

I created a case record at 11:27 AM in my timezone which in turn was 3:27 AM in GMT-05:00. Obviously when I tried System.debug('Case CreatedDate BusinessHours.diff: ' + BusinessHours.diff(defaultHours.Id, updatedCase.CreatedDate, System.now()));, it gave me 0 because both CreatedDate and System.now() were in the same date but before 7:00 AM (GMT-05:00) which is the start of the business hours. The diff() method just didn't count it and it was correct.

On the contrary System.debug('Control BusinessHours.diff: ' + BusinessHours.diff(defaultHours.Id, Datetime.now().addDays(-1), System.now())); showed 10 hours (36000000 miliseconds) because it counted from 3:27 AM (GMT-05:00) of the previous day which spanned the business hours of that day.

You must log in to answer this question.

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