Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    • Top 10 PMD Issues Salesforce Developers Should Focus on in Apex
    • How to Use Graph API for Outlook-Salesforce Connection
    • Enhancing Performance with File Compression in Apex
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Tuesday, May 20
    • Home
    • Architecture
    • Salesforce
      • News
      • Apex
      • Integration
      • Books Testimonial
    • Questions
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Integration Posts
    • Downloads
    • About Us
      • Privacy Policy
    SalesforceCodex
    Home»Salesforce»Apex»Schedule Email alerts based on Business Hours

    Schedule Email alerts based on Business Hours

    Dhanik Lal SahniBy Dhanik Lal SahniSeptember 2, 2021Updated:June 11, 2023No Comments4 Mins Read
    Facebook Twitter Pinterest LinkedIn Tumblr Email
    Schedule Email alerts based on Business Hours
    Share
    Facebook Twitter LinkedIn Pinterest Email

    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

    apex business hour email alert email template Hierarchy Custom Settings invocable method schedule action schedule email alert
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleExtract Driver License Detail from Image using Einstein API
    Next Article Generate OTP in LWC
    Dhanik Lal Sahni
    • Website
    • Facebook
    • X (Twitter)

    With over 18 years of experience in web-based application development, I specialize in Salesforce technology and its ecosystem. My journey has equipped me with expertise in a diverse range of technologies including .NET, .NET Core, MS Dynamics CRM, Azure, Oracle, and SQL Server. I am dedicated to staying at the forefront of technological advancements and continuously researching new developments in the Salesforce realm. My focus remains on leveraging technology to create innovative solutions that drive business success.

    Related Posts

    By Dhanik Lal Sahni6 Mins Read

    Top 10 Salesforce Flow Features of Salesforce Summer ’25

    May 11, 2025
    By Dhanik Lal Sahni6 Mins Read

    Unlock the Power of Vibe Coding in Salesforce

    April 30, 2025
    By Dhanik Lal Sahni5 Mins Read

    How to Implement Dynamic Queueable Chaining in Salesforce Apex

    April 21, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) einstein (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex (3) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (140) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • Top 10 Salesforce Flow Features of Salesforce Summer ’25
    • Unlock the Power of Vibe Coding in Salesforce
    • How to Implement Dynamic Queueable Chaining in Salesforce Apex
    • How to Implement Basic Queueable Chaining in Salesforce Apex
    • How to Suppress PMD Warnings in Salesforce Apex
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • How to Connect Excel to Salesforce to Manage Your Data and Metadata February 9, 2025
    • Difference Between With Security and Without Security in Apex January 2, 2025
    • Top Reasons to Love Salesforce Trailhead: A Comprehensive Guide December 5, 2024
    • How to Utilize Apex Properties in Salesforce November 3, 2024
    • How to Choose Between SOQL and SOSL Queries July 31, 2024
    Archives
    Categories
    Tags
    apex (110) apex code best practice (8) apex rest (11) apex trigger best practices (4) architecture (22) Asynchronous apex (9) AWS (5) batch apex (9) batch processing (4) code optimization (8) code review tools (3) custom metadata types (5) design principle (9) einstein (3) flow (15) future method (4) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (64) lightning-combobox (5) lightning-datatable (10) lightning component (29) Lightning web component (61) lwc (50) named credential (8) news (4) optimize apex (3) optimize apex code (4) Permission set (4) Queueable (9) rest api (23) S3 Server (4) salesforce (140) salesforce apex (46) salesforce api (4) salesforce api integration (5) Salesforce Interview Question (4) salesforce news (5) salesforce question (5) solid (6) tooling api (5) Winter 20 (8)

    Get our newsletter

    Want the latest from our blog straight to your inbox? Chucks us your detail and get mail when new post is published.
    * indicates required

    Facebook X (Twitter) Instagram Pinterest YouTube Tumblr LinkedIn Reddit Telegram
    © 2025 SalesforceCodex.com. Designed by Vagmine Cloud Solution.

    Type above and press Enter to search. Press Esc to cancel.

    Ad Blocker Enabled!
    Ad Blocker Enabled!
    Our website is made possible by displaying online advertisements to our visitors. Please support us by disabling your Ad Blocker.