How to create and deploy a schedule-triggered flow

Michael Halliwell on

Share with

LinkedIn
Twitter

Need help creating and deploying schedule-triggered flows? Here’s a step-by-step guide, written by one of Gearset’s Community Advisors — Michael Halliwell. This article assumes you already have basic knowledge around the logic and data elements of flows.

Where to use a schedule-triggered flow

Schedule-triggered flows launch at specific times and frequencies, helping you to add automation to your Salesforce workflows. This kind of automation can hugely benefit teams in a number of different use cases. For example, let’s say the Accounting department manages the activation and deactivation of accounts in Salesforce. Currently, they’re running reports each week and manually updating the status of accounts to active or inactive based on the contract start/termination dates. Accounting has requested automation that looks at the contract start/termination dates every night and automatically activates or deactivates these accounts — this can be done with a schedule-triggered flow!

Creating a scheduled flow

First, we’ll need to create a new flow and select the Schedule-Triggered Flow option.

The options given to you in Salesforce for selecting a type of flow

Once the Flow Builder opens, click on the Start element. Here, you’ll see two options:

  1. Set Schedule
  2. Choose Object

We’ll want to begin by clicking Set a Schedule as this will allow us to determine the start date, start time, and frequency that the flow will run. For our example, we’ll schedule the flow to start on the 19th of October, and then run every night at 1:30am.

Pop up dialogue box showing the date and time of your schedule-triggered flow

It’s important to note that the Start element is acting like a Get Records element. When this run is triggered, it’s actually going to run this flow for one record at a time. Each record gets stored in a single record variable called $Record, so you won’t need to loop through your results.

Since our Start element is finding our records, we need to choose which object will be queried when the schedule-triggered flow launches. To do this, click on Choose Object. For our example, we’ll select the account object. You can set up criteria to filter down the accounts you query. These are:

  1. None — Run Flow For All Accounts
  2. All Conditions Are Met (AND)
  3. Any Condition Is Met (OR)
  4. Custom Condition Logic is Met

As these options are limited, it’s generally better to add in your logic later in the flow through a Decision element. As a result we’re going to pick None — Run Flow For All Accounts in our Start element, so that all our account records will run through our flow.

Pop up allowing you to choose which object is queried

Once you’ve chosen your object and filter conditions, select Done. You’ve now finished filling out your Start element.

Creating the logic for a schedule-triggered flow

With our Start element completed, we’re going to create the custom logic for our flow.

In this case, we want to automatically update records based on their contract start or termination date. To do this, the flow will first need some logic to find today’s date. Using the New Resource section of the left hand toolbar, let’s create a formula variable that returns today’s date. We’ll use the data type date and type in the formula TODAY(). And we’ll give the API name returnTodaysDateFormula.

Pop up window to create a formula variable to return today's date

Now we have a formula that returns today’s date, we can reference it in our Decision element to see if the account should be terminated or activated.

To do this, we’ll create a Decision element that follows directly after the Start element. Each Decision element automatically has a ‘default outcome’, which is used if the object in the flow doesn’t trigger any changes. You can create different outcomes for your Decision element. We’ll create two here: one for account termination and one for account creation. Let’s take a close look at the logic for each.

Termination logic

We want an account to be terminated if its contract end date is today. Se let’s instruct our Decision element to look at the resource record’s contract end date and check whether this is today’s date. This is where I’ll be referring back to the formula we created earlier, by choosing the operator to be Equals and value to be returnTodaysDateFormula.

Pop up for creating the termination logic

Activation logic

Next, we’ll create a new outcome for contracts that need to be activated. The logic here is the same as that used for the termination outcome, except we’ll access the resource Contract Start Date instead.

Pop up for creating the activation logic

Creating the assignments from our logic

Now that our Decision is created, we’ll need to add two Assignments — one after each decision outcome. Each Assignment will assign a status of active or inactive to the current record in the flow.

Notice that once again we’re referencing the $Record variable.

Termination assignment

The termination Assignment will update the variable $Record Status to Terminated.

Termination assignment of the Decision element

Activation assignment

Now we’ll connect an activation Assignment to our Decision element, for records that need to be activated. Here, we’ll have the variable $Record Status updated to Active.

Activation assignment of the Decision element

The next step is to connect the elements, so the flow looks something like this:

Connect the Start and Decision elements with the assignment

Completing your flow

Finally, we need to add an Update Records element. Both Assignments can terminate to the same Update Records element, as they both reference the same record variable: $Record.

You can choose how the Update Records element finds which records to update. For this example, we’ll select Use the IDs and all field values from a record or record collection. You’ll also need to select which record(s) to update.

Pop up window showing the options when you add the update element to your flow

Once you’ve created your Update Records element, you’ll need to attach both the activation and termination Assignments. The end result will look something like this:

An image of the completed schedule-triggered flow!

You can now save the flow and you’re ready to deploy it!

Deploying the schedule-triggered flow using Gearset

Once you’ve finished creating your schedule triggered flow, it can be deployed along your release pipeline using Gearset.

From Gearset’s Compare and deploy screen, choose your source and target orgs. Click the Metadata comparison filter and then Manage custom filters. In here, we need to check that the filter includes the Flow and Custom object metadata types.

Select flow and custom object in Gearset’s metadata filter

At the comparison screen, select the flow. Gearset will recognize the dependencies and add those to the deployment package.

Select your new schedule-triggered flow in the Gearset comparison screen to deploy

Click Next. From the Deployment summary page, it’s a good idea to validate the deployment by clicking the button in the bottom-right corner — this allows you to see if the deployment will work before committing.

Assuming everything looks fine after validating, you’re ready to hit Deploy now!

Image of a successful schedule-triggered flow deployment with Gearset

Go with the flow!

Building and deploying flows can be tricky, but with help from the community and platforms like Gearset, you can build and release a flow that works for you. To try deploying your schedule-triggered flow, or any other metadata for that matter, definitely give Gearset a go on a free 30-day trial. Happy deploying!

Ready to get started with Gearset?