Based on a business days value (1 = business day 1, 12=business day 12) and taking account of the below I would like to generate a due date based on a created date. - Default business hours, - Standard holidays - Leap year additional days

Namely I create a case on Business day 1. Each case will have a field denoting which business day it is due on, let's take BD5 as an example. When inserting the new record, I would like a Case deadline field to be populated with a datetime value that is 5 business days from the create date at 5pm. I would like those days to exclude non business hours and any holidays I have added into the standard holidays list.


You need to first define Business Hours in this screen considering holidays too.

Business Hours

To calculate and use this Business Hours, Create a separate class for this.

public with sharing class BusinessHoursServices {
    //retrieves & assign the Organization level Business Hours to defaultBH 
    static BusinessHours defaultBH {
        get {
            if (defaultBH == null)
                defaultBH = [SELECT Id FROM BusinessHours 
                            WHERE IsDefault = true];
            return defaultBH;
        private set;

    //it takes the input date and SLA value and return the calcuated date
    public static Datetime getSLATimeByBusinessHour (DateTime input, 
                                Integer SLAhours) {
        return BusinessHours.add(defaultBH.Id,input, 
                            SLAhours* 60 * 60 * 1000L);

And during case creation, calculate end date-time using getSLATimeByBusinessHour method.

For more information, refer BusinessHours class

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.