4

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.

6
  • You might save yourself some trouble by looking at the business hours class. If implemented correctly, you then won't have to deal with weekends and holidays because the class will take those into account for you. Since workflow can send an email, I'm not certain why you feel you need a trigger for this, but you know your use case better than I.
    – crmprogdev
    Mar 28 '15 at 21:31
  • Thanks, I'm completely new to coding so I don't know what the calculation would look like, when combined with the business hours class. I'm not assuming that I do need a trigger, I just need a way to calculate the date/time when the email notification should be sent.
    – Alex S
    Mar 28 '15 at 21:38
  • Methods for the system Business Hours Class are described in the Apex Code Developer's Guide here: salesforce.com/us/developer/docs/apexcode/Content/…. If your business hours are defined in your org, you shouldn't need a trigger for what you describe, just workflow.
    – crmprogdev
    Mar 28 '15 at 21:50
  • Thanks for the link but it doesn't even include an example for adding hours using the class. As for the workflow, how do I tell it that I want to use working hours only? You're suggesting that this is simple but the top contributors on the Success forum haven't been able to come up with the formula for a field update..
    – Alex S
    Mar 28 '15 at 21:56
  • Is there a reason you would not use Escalations? Your business case sounds pretty much exactly what that is for. help.salesforce.com/apex/…
    – CyberJus
    Mar 28 '15 at 22:00
4

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);
2
  • That's much appreciated, I know it looks simple but it's not so easy when you don't know where to start! It looks like I'll need to define custom business hours so I'll change IsDefault=true to Id = '01m7000000008KU'. What does including 32400000 in the code do?
    – Alex S
    Mar 28 '15 at 22:41
  • That is the interval to add in milliseconds. 32400000 should be 9 hours if I can do math right.
    – CyberJus
    Mar 28 '15 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.