Salesforce | Send an email alert to Lead owner after X business hours from Lead Creation timestamp

|
| By Webner

Let’s say our business hours are from 9am to 6pm (Monday to Friday) and if Lead is created at 5pm on Monday an email alert should be sent to LeadOwner at 12am on Tuesday.

You can set your business hours in Salesforce under Company Profile.
1
Above requirement can be achieved by using BusinessHours function in apex.
BusinessHours.add(businessHoursId, startDate, intervalMilliseconds)
Code snippet:

Datetime currentTime = System.now();

Id businessHourId = [SELECT Id FROM BusinessHours WHERE IsDefault = true].id;

Integer hours =4 ;
Datetime emailSendDate = BusinessHours.add(businessHourId, currentTime, hours* 60 * 60 * 1000L);

Leave a Reply

Your email address will not be published. Required fields are marked *