Skip Scheduled Flow on Weekends and Holidays

Skip Scheduled Flow on Weekends and Holidays

Last Updated on March 14, 2022 by Rakesh Gupta

Big Idea or Enduring Question:

We don’t want to work on Weekends or Holidays – we all get that! If so then, do you think our clients or prospects do?

If you are in doubt then try sending an Email to your prospects to buy a home on a Christmas! Or how about sending a contract renewal Email to your customers on a weekend? Success rate if these actions is not too difficult to estimate, is it?

You get my point.

One of the cardinal principals of marketing is to send a soliciting Email to a client or a prospect at the most opportune time – Weekends or Holidays fall well short of fitting the bill.

No wonder then, that one of the most common requests we receive from Ohana is for tips on how to skip triggering Email alerts – or other business process automations – on Weekends or Holidays.

Just as the probability of someone looking to buy a house on Christmas is low. Even lower is a probability that someone will check their corporate Email during a weekend to sign a contract renewal document.

If so then, the question is, how can we skip Scheduled Job on Weekends or Holidays?

Or, how can we skip Scheduled-triggered Flow on Weekends or Holidays?

In other words, is there a way to handle such scenarios in Salesforce? Does Salesforce have features – or tricks – to easily manage such requirements?

Now, we are not known as Salesforce Ninjas for nothing – we neither take, nor give, a ‘no’ for an answer! So, let us get started by wearing our out-of-the-box thinking hat!

Let’s start with a business use case.

Objectives:

This blog post will help us to learn how to apply the following insights and tools to ensure highest ROI on customer touchpoints!:

  • When to use Apex Invocable action with Flow
  • Understand BusinesshHours class and its associated methods
  • Understand @InvocableMethod Annotation
  • How to call an Apex method using Flow to skip triggering Flow on Weekends or Holidays

Business Use case

Warren Mason is a System Administrator at Gurukul on Cloud (GoC). He developed an automated process (that runs every morning at 08:00 am). The process automatically assigns pending approval to the company’s Chief Financial Officer.

The process works like a charm until Warren receives an enhancement request to skip the automated job on Weekends and Holidays. Ouch!!

Prerequisite

Please go through these articles first:

  1. Getting Started with Salesforce Flow – Part 49 (Don’t Let Pending Approval Requests Linger – Reassign!)

Automation Champion Approach (I-do):

While this can be solved using various automation tools like Apex Trigger and others, we will use Salesforce Flow and call an Apex method. To call an Apex method, add the Action element to your Salesforce Flow and select an Apex class with a @InvocableMethod Annotation.

In this article, we will use the call BusinessHours database class from Apex code. Make sure to review it. Use the BusinessHours methods to set the business hours at which your customer support team operates. As of Spring’22 release, it is not possible to directly access BusinessHours method from Flow, that’s why we are using the Invocable apex class.

Before discussing the solution, let me show you a diagram of the process at a high level. Please spend a few minutes going through the following Flow diagram to understand it.
Let’s begin building this automation process.

Guided Practice (We-do):

There are 3 steps to solve Warren’s business requirement using Salesforce Flow. We must: 

  1. Set Business Hour and Holidays
  2. Create Apex class and Test class
  3. Salesforce Flow
    1. Clone an Existing Flow
    2. Create a Variable to store Apex callout outcome
    3. Add action – call BusinessHours method through Invocable Apex
    4. Add a decision element to check if the given date/time is Holiday

Step 1: Setting Up Business Hours and Holidays

  1. Click Setup.
  2. In the Quick Find box, type Business Hours.
  3. Set Business Hours and Holidays, as shown in the following screenshot:

Step 2: Create an Apex class and Test class

Now, we have to understand a new Apex annotation i.e. @InvocableMethod. This annotation lets us use an Apex method as being something that can be called from somewhere other than Apex.

The CustomBusinessHourAndHolidayHandler class contains a single method that is passing the Date/Time to find if specified target date/time occurs within business hours including holidays. Create the following class in your organization.

  1. Click Setup.
  2. In the Quick Find box, type Apex Classes.
  3. Click on the New button.
  4. Copy code from GitHub and paste it into your Apex Class.
  5. Click Save.

Step 3.1: Salesforce Flow – Clone an Existing Flow

  1. Click Setup.
  2. In the Quick Find box, type Flows.
  3. Select Flows then click on the Flow Namethat will be modified. In this case Reassign Pending Approval Requests.
  4. Click on the Save As button at the top right of the Flow detail page.
  5. In the pop-up window enter the Name. It is recommended to update the description with a note about what changed. The API Name is not updateable.
  6. For API Version for Running the Flow always select the latest version, in this scenario, 54.
  7. Click Done.

Step 3.2: Salesforce Flow – Create a Variable to Store Apex Callout Outcome

  1. Under Toolbox, select Manager, then click New Resource to store apex callout outcomes.
  2. Input the following information:
    1. Resource Type: Variable
    2. API Name: varB_IsHoliday
    3. Data Type: Boolean
    4. Default Value: {!$GlobalConstant.False}
    5. Check Available for Input
    6. Check Available for Output
  3. Click Done.

Step 3.3: Salesforce Flow – Adding an Action to Call BusinessHours Method Through Invocable Apex

  1. On Flow Designer, click on the + icon and select the Action element.
  2. Select the CustomBusinessHourAndHolidayHandler Apex class.
  3. Enter a name in the Label field- the API Name will auto-populate.
  4. Set Input Values:
    1. Field: Date/Time Field
    2. Value: {!$Flow.CurrentDateTime}
  5. Store Output Values

    1. Field: outputIsBusinessHour
    2. Value: {!varB_IsHoliday}
  6. Click Done.

Step 3.4: Salesforce Flow – Using Decision Element to Check If Given Date/Time is Holiday

Now we will use the Decision element to check if given date/time is within Business Hours including holidays.

  1. On Flow Designer, click on the + icon and select Decision element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Under Outcome Details, enter the Label the API Name will auto-populate.
  4. Condition Requirements to Execute Outcome: All Conditions Are Met (AND)
    1. Row 1:
      1. Resource: {!$varB_IsHoliday}
      2. Operator: Equals
      3. Value: {!$GlobalConstant.Flase}

In the end, Martin’s Flow will look like the following screenshot:

Almost there! Once everything looks good, click the Activate.

Formative Assessment:

I want to hear from you!

What is one thing you learned from this post? How do you envision applying this new knowledge in the real world?  Feel free to share in the comments below.

Proofreader: - Munira Majmundar
Have feedback, suggestions for posts, or need more information about Salesforce online training offered by me? Say hello, and leave a message!

5 thoughts on “Skip Scheduled Flow on Weekends and Holidays

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.