Activity › Forums › Salesforce® Discussions › What is BusinessHours Class in Salesforce Apex? And how we can use them?
-
What is BusinessHours Class in Salesforce Apex? And how we can use them?
Posted by Avnish Yadav on August 16, 2018 at 6:33 AMWhat is BusinessHours Class in Salesforce Apex? And how we can use them?
Parul replied 7 years, 8 months ago 4 Members · 3 Replies -
3 Replies
-
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.
- [adinserter block='9']
-
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.
-
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.