Salesforce provides a means to configure business hours and holidays in the system. Admin can setup business hours for their company by simply navigating to Setup -> Company Profile -> Business Hours. The business hour configuration ensures that the system is aware of the working hours and accordingly computes target date (such as SLA of cases) and helps you to provide the correct support to your customer.
So far this feature proves to be awesome, but the problem arises when one needs to access holidays associated with a given business hour or vice-versa in apex code. As of Winter '16, Salesforce currently do not expose the relationship between Business Hours and Holiday via apex code. The only way to determine all the holidays for a given business hour is to navigate to Setup -> Company Profile -> Business Hours and select the required business hour using the required permission set. To understand it better, let us dig into this problem statement considering one genuine business requirement.
Consider a hypothetical bank named YourMoney which is one of the leading banks spread globally and uses the Salesforce platform for its operation. YourMoney intends to display a home page component with the list of holidays in the current fiscal year for all its users. Additionally, they also want to send out email notifications to employees for any upcoming holiday a week before so that they can plan out their work accordingly. Had there been access to the relationship between business hours and holiday standard objects, it would have been an easy task for developers. But because Salesforce does not expose this relationship via apex code, we'll have to devise our own way of reading this relationship.
To address the above mentioned shortcomings of Salesforce, the BusinessHoursHoliday retriever component comes to the rescue of Salesforce developers.
BusinessHoursHoliday retriever provides two static methods to retrieve a list of all holidays associated with a business hour record and vice-versa. These two methods exploit the strong coding convention followed by Salesforce to display a related list as a page block table by performing string parsing on a related list of holidays and business hours to retrieve the corresponding record id and then performs a SOQL query to retrieve all records.
Let's now dig into details of these two methods.
1] retrieveHolidays( BusinessHours bhr )
2] retrieveBusinessHrs( Holiday hday )
While this component proves to be handy in most of the use cases, at times it may not be able to retrieve all holidays or business hours. This is because this component can retrieve at most 100 holidays associated with a business hour and vice-versa. If at all there is a need to access more than 100 holidays associated with business hour or more than 100 business hours associated with a holiday record, then developers would need to update the corresponding static count defined in constant class to a value that would be good enough to retrieve all records.
Also, this solution is not the elegant one in the sense that it tries to parse the page block table of related holidays and business hours and is bound to break if at all there is a change in the generated page block table of Salesforce related list. But given the fact that there is no access to the relationship between business hours and holiday, this is the only way to read this relationship.
Unmanaged Package URL:
You can download the unmanaged packaged version of this component using below link