In our org, I have set-up business hours as 08:00-16:00 and timezone is GMT +2. Saturdays and Sundays are blank which means they are holidays. enter image description here

I then went in and set-up Holidays and have put the 24th, 25th and 26th of December as full day recurring holidays. enter image description here

Now when I call the following method:

public static date verifySelectedDate (date userSelectedValue){
    BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
    DateTime dt = datetime.newInstance(userSelectedValue, Time.newInstance(12,59,59,59));
    DateTime nextStart = BusinessHours.nextStartDate(bh.Id,dt);
      System.debug('the value for datetime nextstart is '+nextStart);
    date deliveryDate=nextStart.date();
    return deliveryDate;

The issue is that when the userSelectedValue is "2018-12-22" (Saturday) the BusinessHours.nextStartDate method returns:

USER_DEBUG [59]|DEBUG|the value for datetime nextstart is 2018-12-24 06:00:00

This is incorrect as this is configured as a holiday in the system and should be skipped over. The value returned should be 2018-12-27 06:00:00.

Any ideas why the method is not taking holidays into consideration?


Alright, so found out the reason why this was happening. I had forgotten to associate the Holidays with the business hours. I had entered the holidays in the system (System>>Company Information>>Holidays), but had not associated them to my default business hours #facepalm. Had to go into my business hours and then add the holidays using the related list of Holidays on the business hours page.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.