Home SalesforceApex Schedule Email alerts based on Business Hours

Schedule Email alerts based on Business Hours

by Dhanik Lal Sahni

Recently I got a requirement to schedule email alerts based on business hour for any record creation in custom object. Below are use case and requirement.

Use Case :

We have a custom object, when record is created in that object send a email to user after 4 hour of record creation. Email will be sent in business hour only.

Business Hour is set to 10AM-6PM for each working day. Saturday and Sunday are holiday.

Requirement:

  1. If record is created at 5PM then system should sent email at 1 PM next day.
  2. If record is created at 5PM on Friday then system should sent email at 1 PM next Monday.
  3. If record is created at 10AM then system should sent email at 2 PM same day.

Solution:

To solve this use case, we will need to do below tasks

  1. Create a custom field in object to store reminder time.
  2. Create Custom Setting for Reminder Time
  3. Create InvocableMethod class to get next business date
  4. Create a Email Template and Email Alert for Notification
  5. Create process builder to schedule action based on custom field created from step 1
  6. Test functionality

1. Create a custom field in object to store reminder time

Create a custom field in custom/standard object where email need to be scheduled on record creation. Custom object name is Business Hour Email(Business_Hour_Email__c).

Field APIField NameField Type
Email_Trigger_DateTime__cReminder Date TimeDateTime

2. Create Custom Setting for Reminder Time

Create a Custom Setting Schedule Reminder Time (ScheduleReminderTime__c) and add a number field Reminder Hour. We are creating this custom setting to make configurable schedule time. It can be set to 4 hour or 1 hour based on your requirement.

Custom Setting for dynamic reminder time

3. Create InvocableMethod class to get next business date

Create a apex class which will give next business date based on business hour setup for salesforce org. We have reminder time 4 hr, which we can set in above custom setting to make it configurable.

Code Explanation:

CustomSettingService.apxc– This will get custom setting value. We are using default organization value.

BusinessHoursService.apxc – This will get default business hour. You can use different business hour based on your requirement and configuration. This has method getReminderTimeByBusinessHour which will give reminder time based on reminder hour and configured business hour.

BusinessHourEmailSelector.apxc– This is a selector class to get records from custom object BusinessHourEmail.

BusinessHoursController.apxc – This class has InvocableMethod which will be called from process builder to set business hour. All above class are used in this to update schedule reminder datetime.

4. Create a Email Template and Email Alert for Notification

Create a email template to send a notification to user. I have created template Reminder Email. For testing put any content in email template

Create a Email Alert from workflow action setup. Use above created email template in this email alert. Set user to whom you want to send email alert. Set name of email alert Email Reminder.

Email Alert in Workflow action

5. Create process builder to schedule action based on custom field

Create a process builder to schedule email alert. Below action will be performed by process builder

  1. Update Schedule Reminder date time
  2. Schedule Action on that updated reminder date time.

Create a process builder on object Business Hour Email. This process builder will first update reminder time for that record and then a schedule action will send email 0 hour after from Email_Trigger_DateTime__c. 0 hours to send email alert immediately.

Process Builder Name: Send Schedule Email to Contact

Process builder to schedule action

Criteria: Set no criteria, Change based on your requirement

Immediate Action – Call invocable apex Set Email Reminder Time which is created in BusinessHoursController class

Call Invocable method from Process builder

Schedule Actions – Set 0 hours After Email_Trigger_DateTime__c field.

Set Schedule Action in process builder

Add Action after Schedule Action – Send Email using Apex or Create and user a Email Alert from workflow action in setup. I have used email alert (Email Reminder) created in above step.

Email Alert in Process Builder

Activate process builder once it is created to run functionality.

6. Test Functionality to Schedule Email Alert

Now try to create record in object Business Hour Email and see Reminder Date Time field value. It will be set according to business hour and user will get email based on reminder time.

Setting Schedule Reminder

References:

Custom Settings Methods

How Does Salesforce Process Scheduled Actions?

Related Posts:

Send Email Using Email Template and Apex

Send Email Template As PDF Attachment using Salesforce Apex

You may also like

Leave a Comment