Activity Forums Salesforce® Discussions What is BusinessHours Class in Salesforce Apex? And how we can use them?

  • Anjali

    Member
    August 17, 2018 at 5:46 am

    Hi Avnish,

    You can use the BusinessHours methods to set the Businesshours at which your customer support team operates.

    The following are methods for BusinessHours. All methods are static.

    • add(businessHoursId, startDate, intervalMilliseconds) - Adds an interval of time from a start Datetime traversing business hours only. Returns the result Datetime in the local time zone.
    • addGmt(businessHoursId, startDate, intervalMilliseconds) - Adds an interval of milliseconds from a start Datetime traversing business hours only. Returns the result Datetime in GMT.
    • diff(businessHoursId, startDate, endDate) - Returns the difference in milliseconds between a start and end Datetime based on a specific set of business hours.
    • isWithin(businessHoursId, targetDate) - Returns true if the specified target date occurs within business hours. Holidays are included in the calculation.
    • nextStartDate(businessHoursId, targetDate) - Starting from the specified target date, returns the next date when business hours are open. If the specified target date falls within business hours, this target date is returned.
  • shariq

    Member
    September 16, 2018 at 8:58 pm

    Hi,

    Example to use BusinessHours class -

     public class BusinessDays {

     private List<Boolean> businessDay = new Boolean[7];
     private List<Time> startHours = new Time [7];
     private List<Time> endHours = new Time [7];
     private Date knownSunday = date.newInstance(2013, 1, 6);

     // Constructor creates businessDay array
     public BusinessDays() {

     BusinessHours bh =
     [Select
     SundayStartTime, MondayStartTime, TuesdayStartTime,
    WednesdayStartTime, ThursdayStartTime, FridayStartTime,
     SaturdayStartTime, SundayEndTime, MondayEndTime,TuesdayEndTime,
     WednesdayEndTime, ThursdayEndTime, FridayEndTime,SaturdayEndTime
     From BusinessHours 
     Where IsDefault=true];

     businessDay[0] = (bh.SundayStartTime != null);
     businessDay[1] = (bh.MondayStartTime != null);
     businessDay[2] = (bh.TuesdayStartTime != null);
     businessDay[3] = (bh.WednesdayStartTime != null);
     businessDay[4] = (bh.ThursdayStartTime != null);
     businessDay[5] = (bh.FridayStartTime != null);
     businessDay[6] = (bh.SaturdayStartTime != null);
     
     startHours[0] = bh.SundayStartTime;
     startHours[1] = bh.MondayStartTime;
     startHours[2] = bh.TuesdayStartTime;
     startHours[3] = bh.WednesdayStartTime;
     startHours[4] = bh.ThursdayStartTime;
     startHours[5] = bh.FridayStartTime;
     startHours[6] = bh.SaturdayStartTime;

     endHours[0] = bh.SundayEndTime;
     endHours[1] = bh.MondayEndTime;
     endHours[2] = bh.TuesdayEndTime;
     endHours[3] = bh.WednesdayEndTime;
     endHours[4] = bh.ThursdayEndTime;
     endHours[5] = bh.FridayEndTime;
     endHours[6] = bh.SaturdayEndTime;

     }

     // Check if today is a business day
     public Boolean isBusinessDay(Date inputDate) {
     // index i is index into the businessDay array based on inputDate
     Integer i = Math.mod(Math.abs(this.knownSunday.daysBetween(inputDate)),7);
     return (businessDay[i]);
     }
     
     // Get the start time
     public Time getStartTime(Date inputDate) {
     Integer i = Math.mod(Math.abs(this.knownSunday.daysBetween(inputDate.date)),7);
     return (startHours[i]);
     }

     // Gets next business day, skipping non business days
     public Date nextBusinessDay(Datetime inputDatetime) {
     Integer i =
     Math.mod(Math.abs(this.knownSunday.daysBetween(inputDatetime.date())),7);
     Datetime returnDate = inputDatetime;
     while (!businessDay[Math.mod(i, 7)]) {
     i++;
     returnDate = returnDate.addDays(1);
     }
     return returnDate.date;
     }

     }

    Hope it helps.

  • Parul

    Member
    September 29, 2018 at 1:16 am

    The following are methods for BusinessHours. All methods are static.

    add(businessHoursId, startDate, intervalMilliseconds) – Adds an interval of time from a start Datetime traversing business hours only. Returns the result Datetime in the local time zone.
    addGmt(businessHoursId, startDate, intervalMilliseconds) – Adds an interval of milliseconds from a start Datetime traversing business hours only. Returns the result Datetime in GMT.
    diff(businessHoursId, startDate, endDate) – Returns the difference in milliseconds between a start and end Datetime based on a specific set of business hours.
    isWithin(businessHoursId, targetDate) – Returns true if the specified target date occurs within business hours. Holidays are included in the calculation.
    nextStartDate(businessHoursId, targetDate) – Starting from the specified target date, returns the next date when business hours are open. If the specified target date falls within business hours, this target date is returned.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos