Skip to main content
1 of 3
Ravi
  • 33
  • 5

How to trigger email alert exactly after 4 business hours from Task Created Date

We have a requirement to send an email alert to users after 4 business hours from Task Creation Date ie., Our Business hours are from 7am to 5pm (Monday to Friday) and if Task is created at 4pm on Monday, we need to send an email at 11am on Tuesday. Because only 1 business hour was left on Monday and remaining 3 hours on next business day.

Business hours in our Org: enter image description here

We have created a custom Date/Time field on Task Object named "Task_Closure_Date_Time__c" and trying to populate it's value by adding 4 business hours to Task Created Date as shown below.

Logic in Task trigger handler class:

public with sharing class TaskTriggerHandler { for(Task newObjTask:[Select ID,CreatedDate,Task_Closure_Date_Time__c From Task Where Id IN:taskIdMap.keySet()]) { newObjTask.Task_Closure_Date_Time__c = BusinessHoursServices.getSLATimeByBusinessHourGmt(newObjTask.CreatedDate,4); updatedTaskList.add(newObjTask);
} }

BusinessHoursServices class:

public with sharing class BusinessHoursServices { static BusinessHours defaultBH { get { if (defaultBH == null) defaultBH = [SELECT Id FROM BusinessHours WHERE IsDefault = true]; return defaultBH; } private set; } public static Datetime getSLATimeByBusinessHourGmt (DateTime input, Integer SLAhours) { return date.valueof(BusinessHours.addGmt(defaultBH.Id,input, SLAhours 60 * 60 * 1000L));* } }

Now the returned "Task_Closure_Date_Time__c" value is falling on same day i.e., on "Created Date" irrespective of no of SLA hours passed. It is not considering the next business day. (hours part of Time is getting changed but Date remains the same)

We would like to use this Custom Date/time field in process builder to send an email alert.

Please let me know how to calculate the correct Date/Time value by taking business hours into consideration.

Many thanks in advance, Raviteja

Ravi
  • 33
  • 5