• #31794
    Forcetalks

    What is BusinessHours Class in Apex? And how we can use them?

    What is BusinessHours Class in Apex? And how we can use them?

     

    #31896
     Anjali #6
    Forcetalks

    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.
    #34623
    Salesforce | shariq shariq #1
    Forcetalks

    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.

    #39923
    Salesforce | Parul Parul #2
    Forcetalks

    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.

    #39942
    Salesforce | shariq shariq #1
    Forcetalks

    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.

Viewing 5 posts - 1 through 5 (of 5 total)

Please to reply to this topic.

CONTACT US

We're not around right now. But you can send us an email and we'll get back to you, asap.

Sending

About Us

Forcetalks is a Salesforce collaboration platform for coders & developers, geeks & nerds, consultants & business heads, admins & architects, managers & marketers and of course the business owners. A community where you can learn from, where you can contribute to. For you. For Salesforce. Read More...

Copyright 2018 Forcetalks. All Right Reserved.

Log in with your credentials

or    

Forgot your details?