Business hours may need to be calculated for a number of reasons. Your functional area may have set up Service Level Agreements or SLAs that say you must complete a certain activity within a certain time period in order to make the business unit run effectively. A related concept is Turn Around Time or TAT, where maybe a government agency has created a hard rule or law that must be followed for finishing a certain activity.
A requirement could be that a certain piece of paperwork must be processed within 4 business hours. That statement seems pretty simple until you start taking into account how you can possibly do that calculation for business hours. First, you should clarify that the calculation is not really just calendar hours. A calculation for calendar hours is usually much simpler than for business hours in that you can usually just add hours using a simply date.addHours() function in Salesforce Apex. Even with calendar hours you still need to take into account things like weekends and holidays, but you still do not have to deal with the business hours too.
A first question to ask during the requirements gathering is whether the business hours are the same across all business units. The nice part is that if hours are the same or different that Salesforce can manage that requirement if we use the Business Hours functionality. If you go to the Setup menu in Salesforce and then drill down through Administer, Company Profile you will get to Business Hours. This is a point-n-click feature that is used with other features like escalation rules and entitlement processes. But we can also use it for helping us to figure out our own SLA or TAT rules.
You can setup one set of Business Hours to be the default hours for the company. Then other Business Hours can be setup to be used for specific business units. How would you tie a set of Business Hours to a specific Account for example? It is really simple actually. You can just create a lookup field on the Account object to the Business Hours object. Then each account could have its own set of Business Hours.
So now we have a set of Business Hours for each account, but how do I add 4 business hours to a date field that I might have on my Account record?
This can be done with a before trigger on the Account object and the solution leverages some built-in functions in Apex.
The first Apex feature to recognize is the BusinessHours class which has a method called BusinessHours.addGMT(). DateTime values are returned in Salesforce Apex in GTM so using the addGMT() method makes the most sense. It is really easy to use. You first just need to figure out which Business Hours you need to use. So if you have your AccountID and you have setup a lookup field on the Account object to BusinessHours your query could be something like…
Account myAccount = [Select ID, DateToAddHours__c, BusinessHours__c From Account Where ID = :accountID];
You could then add your 4 hours to that date on the Account object with…
dateTime myNewDateTime = BusinessHours.addGMT(myAccount .BusinessHours__c, myAccount.DateToAddHours__c, 1000 * 60 * 60 * 4);
Notice in the formula below that the last parameter is the number of milliseconds so the 1000 * 60 * 60 gives you the number of milliseconds in an hour so then we can just add the number of hours to multiply onto the end.
So in the end if values for our computation are…
1) Our business hours are 8-5pm Monday through Friday
2) We chose 3pm to be our starting date
3) We want to add 4 hours to this starting date
The result will be 10am on Monday. This is because we have two hours left on Friday and we need to add two more hours onto Monday.
This actually does take into account any weekend calculations so we do not have to deal with those either. The only thing left to take into account are any holidays. But what is great is that holidays can be added right to the Business Hours when they are setup in Salesforce. There is a whole Holidays feature in Salesforce that can even set up recurring holidays.
That is great! So basically with one line of code in an Apex Trigger we can determine some great calculations regarding Business Hours.
If we can help you setup any SLA or TAT type rules in your Salesforce org please let us know at Sundog!