Schedule Flows | Send Emails using Flow in Salesforce

In this blog I am trying to showcase one powerful and useful feature of the flows i.e. Scheduling Flows.We will see how to Schedule Flow in Salesforce

How to schedule flows in salesforce
Though this blog is mainly about Scheduling Flows, but we can get an idea about three important things here:

       1. How to Schedule Flow in Salesforce?

       2. How to Send Emails from Flows?

Some Background: 

Scheduled Flows were released in Winter ‘20. They give you the ability to run an auto-launched flow (one that doesn’t have a user interface screen) on a daily or weekly schedule.

Sample Use Case:
As a Customer service manager/supervisor who is not using Salesforce actively, I want to receive an email daily with information like total open cases(if there are any), closed cases etc. in the Salesforce org.
For the scope of this blog, I am only considering the count of open cases scenario but same can be customised and enhanced to include any other details as well.

Overview of the Solution:
Schedule Flow in Salesforce

Start Element: Like all flows, we have a “Start” element. This allows us to set the rules around when the flow runs; we will use it to schedule the flow and choose our starting data. 

Get Records Element: We will use this element to fetch all the existing open cases in the org based on our condition.

Decision Element: This element is used to check if any open cases were found by the Get Record Element in the previous step. This is an important step because it ensures that flow will only run further if there are any open cases in system.

Assignment Element: If open cases are found, this element will assign the Count of total open cases to a variable which will be used later as a merge field in the email template being used to send the email to supervisor.

Action Element: This is where all the action takes place and an email is sent to the service manager with the count of open cases on daily/weekly basis.

Now let's dive into the play by play action with steps to create the flow.

1. Start Element: 


Double Click on the Start Element after choosing to create a new Auto-launched flow.
Select the scheduled jobs option. Provide details like start date, time and frequency.
Note: Start date has to be in future otherwise flow will not run.
Optionally we can choose the object on which the flow will run. This can also be done in the next step when we Get Records.

Schedule Flows in Salesforce

2. Get Records Element:

Drag and drop the Get Record Element from left to the flow builder screen.
Choose the object from which the records have to be found.
Add Conditions: In our case, since we are finding all open cases, the condition is simple IsClosed = False.
To select false, no need to create a new variable as it is available by default in the list of resources.

Note: This will not return a single record and will return an array/list of records since we will select All Records in the How many records to save option as shown in screenshots below.






After saving the element ,connect Start element with Get Records Element.
Schedule Salesforce Flows

3. Decision Element: 

Create a new decision element by dragging into the flow builder screen. A decision has custom outcomes that we can define and a default outcome. We will define a custom outcome because it is based on a specific condition that Open cases are found by the Get Records Element.
Here we will select the Name of the Get Records Element variable provided in the last step which will be visible by default in the Search resource column.

Schdeule Flows
After selecting that resource, we need to check if this list is empty or not. This can be done by selecting IsNull operator and checking for false as we did in last step for Get Records Element
Note: In the screen Shot below, {!FindOpenCases} is the resource name we provided for GetRecordsElement in the last step.



Join the elements.
Flows schedule in salesforce
4. Assignment Element: 

Create a new Assignment Element. Basically, till now we have got a list of open cases that were found by the Get Records Element. We now need to find the count of the same and assign it to a variable/resource which we can use later to send in the email.


We will need to create a new resource from search variable screen. Click on the + New Resource and create a new Resource of Type variable and Datatype as Number.



After creating the variable, it is automatically chosen as a default to assign values to it. After this we need to select an operator and assign a value to this variable.




Choosing Operator :This is a very important and interesting step. To find and assign the count for any list of records directly  there is an out of box option already provided in the flows.This is under operator and named as "Equals Count"
This is very useful otherwise we would had to run a loop on the case records and assign count value using iterations.
Just assign this newly created variable value as a count of Records returned/found from Get Records Element {!FindOpenCases}  in the first step and this will automatically store the count of cases found by the Get Record element in the previous steps.



After this join the elements.
5. Action Element: 

This is where we create the Email Send Capability from Flows.Action component has many action options like record deletions, insert, tasks etc.
We will use the Send Email action in the same.
To directly find the Email Send Action, search for the Email in the search bar.

Now we need to provide different Email Attributes just like any other email Alert in Salesforce.
Subject,Body and from and To Addresses.
Body: I have created a new Email Template using the New Resource feature.
Note: We can directly provide the text in this field too instead of creating a new resource for Body.

Important: Salesforce provides the Text Template type resource which is very useful for this type of action. I have chosen this option to create a sample Email Template.
Also, we can have Html/Rich text template and Plain Text. I have currently chosen Plain Text Template.


Use of Count variable as the merge field: This is the most important step. Here we will include the  Count variable created and assigned earlier as a text in out email body template. Click on Insert a resource below body and select the variable we created earlier VarOpenCases. This will automatically be added to the template and we cna change the position of the same within the text.

Subject: Similarly for the subject I have created a new resource of type text and provided a sample text value.


To Address: Provide comma separated to address.




From/Sender Address: This is tricky and important. If not setup correctly, it causes issues and no emails are sent.

In my experience its good to have a from address as a Org wide Email Address. To setup an org wide address as From Email address first select the Sender Type as "OrgWideEmailAddress" and then provide the Sender Address as any existing org wide address which you need to use for this email.

Connect this element with the previous one and we are now done.

There are some Important Considerations while using Scheduled Flows in Salesforce. Please find them here:

Just save and Activate and Flow is ready to test.
Hope this helps!

Please comment in case of any suggestions, issues or any feedback.

Comments

  1. Have you ever been able to send an Email Alert in flow? I find using the $Record.Id actually does not work.

    ReplyDelete

Post a comment

Thanks for your Feedback!


Never Miss Latest Posts