BusinessHours Add Function returns incorrect results for 24 hours time window.
Last updated 2022-02-10 ·Reference W-1617948 ·Reported By 3 users
Summary
BusinessHours.addGMT() function returning incorrect results when business hour are set to 24 hrs in the following scenario.
Scenario 1- "SUBTRACTING" days from a particular given date, it is not skipping the non business hours ( Saturday and Sunday )
Scenario 2- "ADDING" days to a particular given date, when date is set as midnight eg,Datetime.newInstance(2013, 3, 27, 0 , 0, 0), it is not skipping the non business hours ( Saturday and Sunday )
Repro
Setup
- set of business hours defined with no Business hours on Sunday and Saturday .
- The rest of the days have a 24 hour schedule.
Steps to replicate
- Run the following code in developer console
Scenario 1-
Apex Code-
BusinessHours stdBusinessHours = [select id from businesshours WHERE IsDefault=true];
if ((stdBusinessHours != NULL)) {
DateTime dt = Datetime.newInstance(2013, 3, 27, 0 , 1, 0);
System.debug(dt);
System.debug('****After adding 3 Business days ****' +BusinessHours.addGMT (stdBusinessHours.id, dt, 3 * 24 * 60 * 60 * 1000L).format());
System.debug('*****After Subtracting 3 Business days****'+BusinessHours.addGMT (stdBusinessHours.id, dt, -3 * 24 * 60 * 60 * 1000L).format());
}
Results-
Start Date - dt - 2013-03-27 07:01:00 (Wednesday)
|****After adding 3 Business days ****4/1/2013 12:01 AM ( Monday ) - Business Hour . This is the expected behavior
|*****After Subtracting 3 Business days****3/23/2013 12:01 AM (Saturday) - Non Business Hour . This is the incorrect behavior
Scenario 2-
Apex Code-
BusinessHours stdBusinessHours = [select id from businesshours WHERE IsDefault=true];
if ((stdBusinessHours != NULL)) {
DateTime dt = Datetime.newInstance(2013, 3, 27, 0 , 0, 0);
System.debug(dt);
System.debug('****After adding 3 Business days ****' +BusinessHours.addGMT (stdBusinessHours.id, dt, 3 * 24 * 60 * 60 * 1000L).format());
System.debug('*****After Subtracting 3 Business days****'+BusinessHours.addGMT (stdBusinessHours.id, dt, -3 * 24 * 60 * 60 * 1000L).format());
}
Results-
Start Date - dt - 2013-03-27 07:01:00 (Wednesday)
|****After adding 3 Business days ********3/30/2013 12:00 AM (Saturday) - Non Business Hour . This is the incorrect behavior
|*****After Subtracting 3 Business days****3/23/2013 12:01 AM (Saturday) - Non Business Hour . This is the incorrect behavior
Workaround
Use a non 24 hours business hours
Reported By (3)
Is it Fixed?
Any unreleased services, features, statuses, or dates referenced in this or other public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make their purchase decisions based upon features that are currently available.