Red Argyle Logo

Patterns
The Salesforce Blog with Tailored Goodness

Salesforce Business Hours with Holidays Class

Have you ever had the need to further define Salesforce business hours? Salesforce supports business hours, but what if the requirement is the need to set a date a few business days away from another date or a holiday? I ran into this requirement on a recent project. After a bit of research, I found a blog post by Scott VonSchilling at Internet Creations about business days which does a great job of looking at business days based on business hours.

Scott’s post definitely got me on the right track, but it was missing holidays. Salesforce also supports holiday configuration – you can read about that here. To allow for easy management in the future, I decided to include this configuration in my solution. Ultimately, I needed to check any business day and find out if it was a holiday, and if it wasn’t, then retrieve the business hours.

I ended up creating a BusinessHoursMath class that served my need and also has plenty of room for future functionality expansion. Here is a situation in which this class can really come in clutch – given a Date, I want to get a new DateTime returned for the end of the day 1 business day after the Date that was provided. If it’s a Monday, I would get Tuesday at 5 p.m. If I wanted 5 business days after Monday, I would get back 5 p.m. the following Monday, and so on. But what if there is a holiday on Thursday of that week that my organization has off? As long as that holiday is configured in Salesforce, my class will treat Thursday as a non business day and would then return 5 p.m. Tuesday.

To use, simply setup business hours and holidays in Salesforce and make a call to the class like so:

In the example above, the expireDateTime variable is set to the end of the business day 2 days from today’s date. ENJOY! The BusinessHoursMath class is hosted on GitHub.