Close Menu
SalesforceCodex
    Facebook X (Twitter) Instagram
    Trending
    • The Ultimate Guide to Data Cleanup Techniques for Salesforce
    • How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI
    • Top Mistakes Developers Make in Salesforce Apex Triggers
    • Introducing Agentforce3 to Salesforce Developers
    • The Ultimate Guide to Apex Order of Execution for Developers
    • How to Handle Bulkification in Apex with Real-World Use Cases
    • How to Confidently Manage Transactions in Salesforce Apex
    • Building a Dynamic Tree Grid in Lightning Web Component
    Facebook X (Twitter) Instagram
    SalesforceCodex
    Subscribe
    Saturday, August 2
    • Home
    • Salesforce Platform
      • Architecture
      • Apex
      • Lightning Web Components
      • Integration
      • Flows & Automation
      • Best Practices
      • Questions
      • News
      • Books Testimonial
    • Industries
      • Artificial Intelligence
    • Hire Me
    • Certification
      • How to Prepare for Salesforce Integration Architect Exam
      • Certification Coupons
    • Downloads
      • Salesforce Release Notes
      • Apex Coding Guidelines
    • About Us
      • Privacy Policy
    • Contact Us
    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

    How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI

    July 28, 2025
    By Dhanik Lal Sahni7 Mins Read

    Top Mistakes Developers Make in Salesforce Apex Triggers

    July 25, 2025
    By Dhanik Lal Sahni14 Mins Read

    The Ultimate Guide to Apex Order of Execution for Developers

    July 20, 2025
    Add A Comment
    Leave A Reply Cancel Reply

    Ranked #1 Salesforce Developer Blog by SalesforceBen.com
    SFBenTopDeveloper
    Ranked #4 Salesforce Developer Blog by ApexHours.com
    ApexHoursTopDevelopers
    Categories
    Archives
    Tags
    apex (116) apex best practices (5) apex code best practice (10) apex code optimization (6) Apex logging (4) apex rest (11) apex trigger best practices (6) architecture (22) Asynchronous apex (9) AWS (5) batch apex (10) best code practice (4) code optimization (9) custom metadata types (5) design principle (9) flow (16) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (66) lightning-combobox (5) lightning-datatable (10) lightning component (32) Lightning web component (64) lwc (53) named credential (8) news (4) optimize apex (5) optimize apex code (6) optimize apex trigger (5) Permission set (4) Queueable (9) queueable apex (4) rest api (23) salesforce (150) salesforce apex (52) salesforce api integration (5) Salesforce Interview Question (5) 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

    MailChimp

    Expert Salesforce Developer and Architect
    Ranked #1 SALESFORCE DEVELOPER BLOG BY SALESFORCEBEN.COM
    Featured on Top Salesforce Developer Blog By ApexHours
    Recent Posts
    • The Ultimate Guide to Data Cleanup Techniques for Salesforce
    • How to Leverage Model Context Protocol (MCP) to Enhance Salesforce AI
    • Top Mistakes Developers Make in Salesforce Apex Triggers
    • Introducing Agentforce3 to Salesforce Developers
    • The Ultimate Guide to Apex Order of Execution for Developers
    Ranked in Top Salesforce Blog by feedspot.com
    RSS Recent Stories
    • Top 10 Salesforce CRM Trends to Watch in 2025 July 18, 2025
    • Discover the Top 10 Salesforce AppExchange Apps to Boost Productivity July 10, 2025
    • Top 20 Salesforce Data Cloud Interview Questions & Answers for Admins June 5, 2025
    • 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
    Archives
    Categories
    Tags
    apex (116) apex best practices (5) apex code best practice (10) apex code optimization (6) Apex logging (4) apex rest (11) apex trigger best practices (6) architecture (22) Asynchronous apex (9) AWS (5) batch apex (10) best code practice (4) code optimization (9) custom metadata types (5) design principle (9) flow (16) google (6) google api (4) integration (19) integration architecture (6) lighting (8) lightning (66) lightning-combobox (5) lightning-datatable (10) lightning component (32) Lightning web component (64) lwc (53) named credential (8) news (4) optimize apex (5) optimize apex code (6) optimize apex trigger (5) Permission set (4) Queueable (9) queueable apex (4) rest api (23) salesforce (150) salesforce apex (52) salesforce api integration (5) Salesforce Interview Question (5) 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.