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:
- If record is created at 5PM then system should sent email at 1 PM next day.
- If record is created at 5PM on Friday then system should sent email at 1 PM next Monday.
- 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
- Create a custom field in object to store reminder time.
- Create Custom Setting for Reminder Time
- Create InvocableMethod class to get next business date
- Create a Email Template and Email Alert for Notification
- Create process builder to schedule action based on custom field created from step 1
- 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 API | Field Name | Field Type |
Email_Trigger_DateTime__c | Reminder Date Time | DateTime |
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.
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.
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
- Update Schedule Reminder date time
- 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
Criteria: Set no criteria, Change based on your requirement
Immediate Action – Call invocable apex Set Email Reminder Time which is created in BusinessHoursController class
Schedule Actions – Set 0 hours After Email_Trigger_DateTime__c field.
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.
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.
References:
How Does Salesforce Process Scheduled Actions?