Saturday, September 23, 2023
HomeUncategorizedDiscover Subsequent Enterprise Day From a Specified Date

Discover Subsequent Enterprise Day From a Specified Date


Final Up to date on April 7, 2023 by Rakesh Gupta

Large Concept or Enduring Query:

We don’t need to work on Weekends or Holidays – all of us get that! In that case then, do you suppose our shoppers or prospects do?

In case you are doubtful then attempt sending an Electronic mail to your prospects to purchase a house on a Christmas! Or how about sending a contract renewal Electronic mail to your prospects on the weekend? Success charge if these actions isn’t too troublesome to estimate, is it?

You get my level.

This weblog is a sequel to my earlier weblog, Skip Scheduled Flow on Weekends and Holidays, the place we mentioned learn how to discover out if the required goal date happens inside enterprise hours. This text will go one step additional and clarify learn how to decide the following enterprise day from the given date by contemplating enterprise hours and holidays.

Let’s begin with a enterprise use case.


On this article, the reader will study the next:

  • When to make use of Apex Invocable motion with Movement
  • Perceive BusinesshHours class and its related strategies
  • Perceive @InvocableMethod Annotation
  • name an Apex methodology utilizing Movement to search out out subsequent date when enterprise hours are open

Warren Mason is a System Administrator at Gurukul on Cloud (GoC). He created automation to ship the automated e-mail notification to the shopper after alternative creation.

The method works like a appeal till Warren receives an enhancement request to ship e-mail alerts on the corporate’s working hours. Ouch!! We are going to assist Warren to search out out the following enterprise day and retailer it in a customized subject Next_workday__c on the alternative object.

Automation Champion Strategy (I-do):

Whereas this may be solved utilizing varied automation instruments like Apex Set off and others, we’ll use Salesforce Movement and name an Apex methodology. To name an Apex methodology, add the Motion factor to your Salesforce Movement and choose an Apex class with a @InvocableMethod Annotation.

On this article, we’ll use the decision BusinessHours database class. Make certain to overview it. Use the
nextStartDate methodology that returns the following date when enterprise hours are open. If the required goal date falls inside enterprise hours, this goal date is returned. As of Spring’23 launch, it’s not potential to immediately entry BusinessHours methodology from Movement, that’s why we’re utilizing the Invocable apex class.

Earlier than discussing the answer, let me present you a diagram of the method at a excessive degree. Please spend a couple of minutes going by means of the next Movement diagram to know it.

Let’s start constructing this automation course of.

Guided Observe (We-do):

There are Four steps to unravel Warren’s enterprise requirement utilizing After-Save File-Triggered Movement. We should:

  1. Set Enterprise Hours and Holidays
  2. Create a customized subject on the Alternative object to retailer Subsequent Workday
  3. Create Apex class and Take a look at class
  4. Salesforce Movement
    1. Outline movement properties for record-triggered movement
    2. Add motion – name NextStartDate Invocable Apex Class
    3. Add Replace Information factor to the replace the chance

Step 1: Setting Up Enterprise Hours and Holidays

  1. Click on Setup.
  2. Within the Fast Discover field, kind Enterprise Hours.
  3. Set Enterprise Hours and Holidays, as proven within the following screenshot:

Step 2: Create a Customized Area on the Alternative Object to Retailer Subsequent Workday

  1. Click on Setup.
  2. Within the Object Supervisor, kind Alternative.
  3. Choose Fields & Relationships, then click on New.
  4. Choose Date/Time as Knowledge Kind, then click on Subsequent.
  5. Enter Area Label and click on the tab key, the Area Identify will populate. 
    1. Enter the small print:
      1. As a finest observe, at all times enter a description and Assist Textual content
      2. Click on on the Subsequent button.
  6. Set the Area-level Safety for the profiles.  
  7. Add this subject to Web page Format.
  8. Click on Save.

Step 3: Create an Apex class and Take a look at class

Now, we have now to know a brand new Apex annotation i.e. @InvocableMethod. This annotation allow us to use an Apex methodology as being one thing that may be known as from Movement and Apex. Invocable strategies are known as with REST API and used to invoke a single Apex methodology. Invocable strategies have dynamic enter and output values and assist describe calls.

The NextStartDate class incorporates a single methodology that’s passing the Date/Time to search out if the goal date is throughout the given enterprise hours, the goal date is returned. The returned time is within the native time zone. Create the next class in your group.

  1. Click on Setup.
  2. Within the Fast Discover field, kind Apex Courses.
  3. Click on on the New button.
  4. Copy code from GitHub and paste it into your Apex Class.
  5. Click on Save.

public with out sharing class NextStartDate {
    @InvocableMethod(label="Finds the following date ranging from the goal date when enterprise hours reopens." iconName="slds:customized:customized63")
    public static Listing<response> calculateNextStartDate(Listing<Request> requestList) {
        Listing<response> finalList = new Listing<response>();
        BusinessHours bh = [SELECT Id FROM BusinessHours WHERE IsDefault=true];
        for (Request req: requestList){  
            response resp = new response();
            resp.nextStart=BusinessHours.nextStartDate(, req.targetDate);
        return finalList;
    public class Request{
        @InvocableVariable(label="Goal Date" required = true)
        public Datetime targetDate;
    public class Response
        @InvocableVariable (label="Subsequent Begin")
        public Datetime nextStart; 

Step 4.1: Outline Movement Properties

  1. Click on Setup.
  2. Within the Fast Discover field, kind Flows.
  3. Choose Flows then click on on the New Movement.
  4. Choose the File-Triggered Movement choice, click on on Create and configure the movement as follows:
    1. Object: Alternative
    2. Set off the Movement When: A file is created
    3. Set Entry Standards
      1. Situation Necessities: None
    4. Optimize the Movement For Motion and Associated Information
  5. Click on Achieved.

Step 4.2: Including an Motion to Name Invocable Apex Class

  1. On Movement Designer, click on on the + icon and choose the Motion factor.
  2. Choose the NextStartDate Invocable Apex class.
  3. Enter a reputation within the Label field- the API Identify will auto-populate.
  4. Set Enter Values:
    1. Area: Goal Date
    2. Worth: {!$File.CreatedDate}
  5. Retailer Output Values
    1. Area: Subsequent Begin
    2. Worth: {!$File.Next_Workday__c}
  6. Click on Achieved.

Step 4.3: Including an Replace Information Factor to Replace the Alternative

The subsequent step is to replace the chance subsequent workday subject. We are going to use the Replace Information factor. 

  1. On Movement Designer, click on on the +icon and choose the Replace Information factor.
  2. Enter a reputation within the Label subject; the API Identify will auto-populate.
  3. For Discover Information to Replace and Set Their Values choose Use the IDs and all subject values from a file or file assortment
  4. Choose File(s) to Replace
    1. File or File Assortment: {!$File}
  5. Click on Achieved.

Ultimately, Warren’s Movement will appear to be the next screenshot:

As soon as all the pieces seems good, carry out the steps beneath:

  1. Click on Save.
  2. Enter the Movement Label the API Identify will auto-populate.
  3. Click on Present Superior.
  4. API Model for Operating the Movement: 57
  5. Interview Label: Subsequent StartDate {!$Movement.CurrentDateTime}
  6. Click on Save.

Virtually there! As soon as all the pieces seems good, click on the Activate.

Proof of Idea

Formative Evaluation:

I need to hear from you!

What’s one factor you realized from this put up? How do you envision making use of this new information in the true world? Be at liberty to share within the feedback beneath.


Source link



Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments