function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
keithpeterskeithpeters 

Problem with the BusinessHours.diff method... only on the 13th of March :-/

Hi,

I created a BusinessHours object that defines the business hours between 2 AM and 3 AM each day (including weekends).

 

So when I run the following code, it usually returns 1800000 (i.e. 30 minutes); Except when I run it for 2011-3-13.

 

    System.debug(BusinessHours.diff(
        [select id from BusinessHours where Name = 'test 2 - 3 every day' limit 1].Id,
        Datetime.newInstance(2011, 3, 13, 2, 0, 0),
        Datetime.newInstance(2011, 3,  13, 2, 30, 0)));

06:46:40.037|METHOD_EXIT|[1]|BusinessHours.diff(String, Datetime, Datetime)
06:46:40.037|USER_DEBUG|[1]|DEBUG|0

 

Note also that I don't have any holidays attached to these business hours.

Any help would be much appreciated, thank you.

VPrakashVPrakash

 

Is your Business Hours set as active. If so use the following query. I think it might work

 

 


select id from BusinessHours where Name = 'test 2 - 3 every day' and active=true
keithpeterskeithpeters

Hi,

Thanks very much for getting back so quickly however the query isn't the issue, the one in my code snippet works but for some reason, the diff method does not return the correct result for that particular day.

 

Can't figure out why that would be, like I said, there are no holidays attached to that BusinessHours object. Do they run with a default set?

 

At any rate, thanks again for the response, I'd tried your change but it didn't make any difference.

VPrakashVPrakash

How did you define your business hours? 

 

Did you include Satuday and Sunday into the business hours?

keithpeterskeithpeters

Yes, I have times for every day. Running the following:

 

 

System.debug([
Select 
	b.MondayStartTime, b.MondayEndTime, 
	b.TuesdayStartTime, b.TuesdayEndTime, 
	b.WednesdayStartTime, b.WednesdayEndTime, 
	b.ThursdayStartTime, b.ThursdayEndTime, 
	b.FridayStartTime, b.FridayEndTime,
	b.SaturdayStartTime, b.SaturdayEndTime, 
	b.SundayStartTime, b.SundayEndTime,
	b.TimeZoneSidKey,
	b.IsActive 
From BusinessHours b
Where Name = 'test 2 - 3 every day']);

 

 

Produces:

 

05:03:15.053|USER_DEBUG|[1]|DEBUG|(BusinessHours:{SundayEndTime=03:00:00.000Z, WednesdayEndTime=03:00:00.000Z, MondayEndTime=03:00:00.000Z, SundayStartTime=02:00:00.000Z, TimeZoneSidKey=America/New_York, WednesdayStartTime=02:00:00.000Z, ThursdayEndTime=03:00:00.000Z, FridayStartTime=02:00:00.000Z, TuesdayStartTime=02:00:00.000Z, TuesdayEndTime=03:00:00.000Z, FridayEndTime=03:00:00.000Z, IsActive=true, SaturdayStartTime=02:00:00.000Z, SaturdayEndTime=03:00:00.000Z, Id=01mA000000059OIIAY, MondayStartTime=02:00:00.000Z, ThursdayStartTime=02:00:00.000Z})