[SalesForce] Calculating date/time x working hours from now

I need to trigger an email notification 9 working hours after a particular field is changed in Salesforce. So if the field is changed on a Friday at 4pm, I need to calculate now() + 7 hours + an extra 48 (working hours are between 9-6).

I want to insert the date/time, when the notification should be sent, in a field called SLA Deadline.

When the field is changed the notification will be scheduled to be sent 0 hours after the SLA Deadline date/time, using the Process Builder, as long as the other Process Builder criteria are met.

If the change is made after 6pm then the date/time would be 6pm the following day (unless the change was made on a Friday – in which case it would be 6pm the following Monday). If the change is made before 9am, then the date/time would be 6pm that day.

I was hoping to use a formula to calculate the value and then insert it using a field update. But if this isn't possible then I'm happy to use code instead.

Best Answer

The easiest way to do this via trigger would be to use Business hours. In this case it would be:

BusinessHours bh = [select Id from BusinessHours where IsDefault=true];
obj.SLA_deadline__c = BusinessHours.add(bh.Id, Datetime.Now(), 32400000);
Related Topic