More Dynamic Way to Schedule a Salesforce Flow with Invocable Schedule Flow

Munawirrahman
7 min readDec 25, 2021

Hi Folks!

As a flow developer, you may already familiar with Schedule-Triggered Flow that Salesforce already provided and if you’re familiar with Apex, you may find it has some limitations compared to Apex Scheduler. The current version of Schedule-Triggered Flow only allows you to run the flow with frequency Once, Daily, and Weekly. You cannot define the seconds of when you want to run your flow. And what if, you want to run a flow 3 times a day? that means you have to create 3 Schedule-Triggered Flows with different run time, and if you want to change the logic, you may have to change all those 3 flows. Well… you may already have found workaround that can solve your business use case with Pause and Decision elements, but as business requirements scaling, you may want other solution that make your flow simpler and more straight-forward just like if you’re doing it with Apex Scheduler.

Invocable Schedule Flow come to the rescue!

Image 00 How this solution works

Image 00 shows you how this solution basically works, the features you can expect from this Invocable method are

  1. Run a flow / subflow with Invocable Method (or Apex Action in Flow), which means you can create a job to run your flow from other flows
  2. You can pass a String / Text variable to the flow you want to run. You have more than 1 variable? you can use a json and do the parsing inside the flow you want to trigger, here’s more info for that.
  3. With cron expression, you can define exactly WHEN (even exact second) and your own frequency to run the flow
  4. You can set whether you want to run your flow in future method or not, if you set it in future method, that means you’ll be able to do callouts before you commit DML transaction in your flow (bye bye Pause Element). As a setback, the flow you want to run may delay a little from the time you specified
  5. Not wanting to trigger the flow from other Flow? don’t worry you create a job to execute your flow from Anonymous Apex Window

Attributes

Input

  1. Label : Cron Expression
    API Name : cronExpression
    Type : String (Mandatory)
    Description : An expression used to represent the time and date the job (or Flow) is scheduled to run. More info and samples about how to formulate cron expression: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
    Example : 0 0 10 ? * MON-FRI (Runs Monday through Friday at 10 AM)
  2. Label : Flow API Name
    API Name : flowApiName
    Type : String (Mandatory)
    Description : Flow API Name that you want to run with the scheduler
    Example : TestDummy_ScheduledFlow
  3. Label : Schedule Job Name
    API Name : scheduleJobName
    Type : String (Mandatory)
    Description : Unique name of the job you want to run, this will be important for you to monitor the status of your flow job
    Example : TestDummy_ScheduledFlow_10AM_MONtoFRI
  4. Label : Flow Namespace
    API Name : flowNamespace
    Type : String (Optional)
    Description : The namespace of your flow, if any
    Example : cool_package
  5. Label : Flow Parameter / Variable Name
    API Name : flowParameterName
    Type : String (Optional)
    Description : The variable name that available for input in the flow you want to run, if any
    Example : recordId / Input_JSON
  6. Label : Flow Parameter / Variable Value
    API Name : flowParameterValue
    Type : String (Optional)
    Description : If flowParameterName does not null, this will be the value of variable that available for input you set on flowParameterName
    Example : 0015g00000CKozcAAD / {“recordId” : “0015g00000CKozcAAD”, “isScheduled” : True}
  7. Label : Run Flow in Future Method, default to False if null
    API Name : runInFuture
    Type : Boolean (Optional)
    Description : if null will be automatically set to false. This attribute decides whether you want to run the flow in future method or not. If true, you will be able to do callout before DML transaction in the flow. As a setback, your schedule will be delayed a little, depends on the workload of your org. More info about future method : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_invoking_future_methods.htm
    Example : False
Image 01 Where to Find your Flow API Name

Output

  1. Label : Job ID
    API Name : jobId
    Type : String
    Description : The job ID that generated for your flow to run
    Example : 08e5g00000MYuQE

How to create a scheduled flow Job

Step 1 : Plan and Create a flow you want to run
Check whether you need to use this extension or not. And if decided you need this, you can start to plan, design, and create the flow. And keep in mind of what you want to achieve from this flow, when you want to run this flow, how many times you want to run this flow in specific frequency, what parameters you should pass, you need to run a future method or not, and how you want the flow job to be triggered. Then, you can go to the next step

Step 2 : Install InvocableScheduleFlow Package
Production : https://login.salesforce.com/packaging/installPackage.apexp?p0=04t5g000000Uh3K
Sandbox : https://test.salesforce.com/packaging/installPackage.apexp?p0=04t5g000000Uh3K
Source Code : https://github.com/munawirrahman/InvocableScheduleFlow
Package includes:
- Apex Classes : InvocableScheduleFlow, ScheduledFlow
- Apex Test Class : InvocableScheduleFlowTest (Covered 100% of classes above)
- Autolaunched Flow : Dummy_ScheduledFlow (necessary for test class)

Step 3 : Instruct the flow to run at specific time and specific frequency
There are 2 ways of instructing how to create a flow job to run, Create the flow Job from other Flow or Create the flow Job from Apex Anonymous Window

How to create a flow job from other flow (or parent flow)
You can create a Screen Flow or an Autolaunched flow that you want to run this flow from. That will be depends on your business specification, remember that this is Invocable Method (or Apex Action) from flow, so you can call this function from Screen Flow or other Autolaunched Flow. For my example, I will use a Screen Flow to formulate a flow Job, and a flow that I want to run is just sending an email to myself with parameter as an input variable.

Image 02 Sample Screen to Formulate a Flow Job
Image 03 Sample Screen to formulate a Flow Job
Image 04 Sample Screen to invoke the InvocableScheduleFlow based on Screen on previous screen
Image 05 Debug screen to formulate Flow Job
Image 06 Debug execution of InvocableScheduleFlow
Image 07 Debug Screen after InvocableScheduleFlow

How to create a flow job from Anonymous Apex Window
Sometimes you may not want to create another flow to create a flow Job, just like when you do it with Schedule Triggered Flow. For that, what you can do is just create a flow job from Anonymous Apex Window, which will create your flow job. Here’s how to do that

Image 08 Go to Developer Console -> Debug -> and click Open Execute Anonymous Window
Image 09 Put the script, set your attributes into the Anonymous Window, and check the Open Log checklist
List<InvocableScheduleFlow.Request> requests = new List<InvocableScheduleFlow.Request>();
InvocableScheduleFlow.Request req = new InvocableScheduleFlow.Request();
req.flowApiName = 'Dummy_ScheduledFlow'; //mandatory
req.cronExpression = '0 45 22 ? * FRI-MON'; //mandatory
req.scheduleJobName = 'TestDummy_ScheduledFlow'; //mandatory
req.flowNamespace = null; //optional
req.flowParameterName = 'parameter'; //optional
req.flowParameterValue = 'This is a value'; //optional
req.runInFuture = false; //will set to false if null or not set
requests.add(req);
List<InvocableScheduleFlow.Result> results = InvocableScheduleFlow.invoke(requests);
System.debug(results[0].jobId); //the job id result
Image 10 Click execute, checklist Debug Only, and you’ll have the Job ID that created to run your flow job
Image 11 Result of the flow Job I created (see the email timestamp)

How to Monitor your Flow Job
since you already created a new Flow Job that will be executed at the time you set, you can monitor when your Scheduled Job will be executed, and the history of the execution.

Image 12 Go to Setup and search for Scheduled Jobs
Image 13 You’ll see the list of Scheduled Jobs and when it will be run. Notice that the Job Name we set on the parameter will appear there
Image 14 Go to setup and search for Apex Jobs
Image 15 You’ll see the current status of the job you created with ScheduledFlow in the Apex Class field

For the list of possible Status, you can go to this source, and if there’s an error with your execution, you’ll see the detail in Status Detail field and also you should see a Flow Interview error email in your Inbox.

How to Abort a Flow Job
As you already able to create a scheduled Flow Job, and that already running just like what you wanted at the frequency you wanted, you may want to cancel that due to various reasons. What you can do to abort that scheduler is by just delete the Job on Scheduled Job menu

Image 16 Click Del at the job you wanted to abort

There you go, now you other dynamic way to schedule your flow

Thank you for spending the time to see my writing that trying to solve salesforce limitations, please clap if you think this is useful and let me know if you have any questions, will try to help you the best as I possibly can :)

P.S : I would definitely recommend you to take a look at best practice and limitations of Salesforce Scheduler that we implement in this solution.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

--

--