I am using the BusinessHours class and am just running some basic tests:

BusinessHours bh = [SELECT Id, ThursdayStartTime FROM BusinessHours WHERE IsDefault = true];
if (BusinessHours.isWithin(bh.Id, scheduledDateTime)){ //this will come up as FALSE if the time isn't between 06:00:00.000Z and 17:00:00.000Z
    system.debug('BusinessHours.isWithin is true:: '  + scheduledDateTime + ', biz hours: ' + bh.ThursdayStartTime);
else {
    schedDateTime = BusinessHours.nextStartDate(bh.Id, scheduledDateTime);
    system.debug('BusinessHours.isWithin() is false:: ' + scheduledDateTime + ', biz hours: ' + bh.ThursdayStartTime);

This prints out: BusinessHours.isWithin is false:: 2018-08-29 07:00:00, biz hours: 06:00:00.000Z

Why would it be false if 7:00am is within the registered business hours?

  • this will come up as FALSE if the time isn't between 7am - 6pm M-F <--- This time that you specified what is the time zone? Is it possible that you are looking at the database time which is GMT? – Halfwarr Aug 9 '18 at 15:16
  • 1
    1. What is the TimeZone set in your Business Hours? 2. Can you also provide how are you constructing the scheduledDateTime? 3. Do you have holidays defined? – Jayant Das Aug 9 '18 at 15:20
  • I am running the tests in PST and the Z is UTC which means that the UTC time is 2:00pm. Am I incorrect with the UTC information? Could the BusinessHours class be running in a different timezone? – Olivia Aug 9 '18 at 15:35
  • @JayantDas - 1. I believe the timezone is UTC 2. scheduledDateTime is being passed from a user input from a flow (this COULD be the issue, as the user input is actually inputting a DATE only. But after some discovery the flow forces a DateTime to be passed which is why I am collecting the scheduledDateTime as a DateTime). Datetime scheduledDateTime = (Datetime)request.inputParameters.get('varStartDate'); – Olivia Aug 9 '18 at 15:41
  • @Olivia do you mean the TimeZone is in GMT as : (GMT+00:00) Greenwich Mean Time (GMT) – Jayant Das Aug 9 '18 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.