+ Start a Discussion
Randy OttRandy Ott 

Access BusinessHoursSettings directly through APEX

Is there a way to directly access the BusinessHoursSettings directly through APEX - without going through the REST API? I need to access this object from a trigger but can't call the REST API from a trigger.
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Randy,

I can't find any documentation about a BusinessHoursSettings object; but you can definitely query the BusinessHours object, which will give you the start and end times for business hours for each day of the week.  See the docs: https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_businesshours.htm

And you can try something like this:

<pre>
SELECT Id, MondayStartTime, MondayEndTime, TuesdayStartTime, TuesdayEndTime, (etc.)
FROM BusinessHours
WHERE IsDefault = true
</pre>
Randy OttRandy Ott
I did see that. I’m actually interested in the Holidays associated with the BusinessHours. We need to track Holidays by state so we are looking at setting up different BusinessHours by state and then adding the state holidays for that state. I can get access to the default Holidays (basically all holidays for all states) through the Holiday class but I can’t get access to Holidays assigned to a specific BusinessHours. Randy Ott Vice President of Technology P: 402.378.7564
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
I've done some research.  Clearly, you can associate Holidays with BusinessHours, so there must be some sort of junction object at play behind the scenes.  But when you describe BusinessHours or Holidays, neither has any child relationships - so there is no access to the junction object.

So, if I were trying to meet your requirement, I would use a naming convention.  For example: for the state of Massachusetts, I would create a BusinessHours record named "Massachusetts", and an associated Holiday record named "Massachusetts - Patriots Day".  Then I could use a query like this:

<pre>
String stateFilter = state + '%';
List<Holiday> stateHolidays = [SELECT Id FROM Holiday WHERE Name LIKE :stateFilter];
</pre>

That's the best I can come up with.  I hope it helps!