Datetime in Flows and Creating Scheduled Emails

Datetime can now be captured in Flows with the Summer ‘18 release, so I wanted write about that since there could a number useful apps and functions that can be built with this one addition. In this post I am going to build a interface using Flows where users can schedule an email to be sent out to leads.

The flow consists of the following items:

  • A screen where user can enter the email subject, email message and the date and time of when they want the email sent.
  • Ability to search for leads (by lead status) and select which leads to receive the scheduled email.

To have a record of the email and store the scheduled date and time we will create a custom object that will store the following fields.

  • A lookup field to the Lead object.
  • A text field to store email address
  • A text field to store email subject
  • A long text field to store the email body.
  • A date time field to store the scheduled email send time.
  • A formula field that calculates a date/time value one hour before the actual date time (to be used to schedule the email).

To facilitate the scheduling and sending of email we will use a process builder flow to handle the following:

  • Based on the lead lookup field, store the lead record’s email address to the scheduled email object.
  • A scheduled action to send out the email.

This is the general idea of how we are going to approach building this email scheduling interface. Let’s go flow and process in detail.

The Flow

Entering email details, scheduled date / time, and searching for leads

The first part we will cover is how users can write and schedule an email along with searching for recipients from lead records.

The first field of the first screen will be accept the schedule date and time. As you can see from the screen shot we are using the Date/Time input type. The second and third input fields are text and text area fields for the user to enter the email subject and message.

The next screen will allow the user to search for lead records by lead status. The Lead Status input choice field will pull picklist values directly from the Status field of the Lead object.


Once a lead status has been selected, the next step will be to query all leads where the Status field matches what was selected in the input choice field. Results will be stored in the LeadRecs sObject collection variable. We will be storing Id, FirstName, LastName, Status, Email, and Company field values.

Processing Lead search results and presenting in a table

If there are matching leads returned from the search we will present it on the next screen. For this example I will be using a lightning component I built that accepts data from a flow and displays a lightning:datatable component on a flow screen. Check out my initial blog post on how I built the component and its usage here and here.Check out my initial blog post

Note that the original lightning component discussed in my previous post only allowed one row to be selected. I have now updated the component code to now allow multiple row selection. Changes I have made are available in the Github repo for the code.


To handle processing the lead records and prepare them to be displayed in the component, I will be looping through each record. Each of record will be stored in a text variable where each field value will be comma separated and storing each record into a collection variable of type text.

The collection variable of lead record data will be passed to the flow data table Lightning component where it will be parsed and displayed in the next screen. We also pass column details and maximum of rows that can be selected.

Users can select the leads they want to include in the email by checking the box on each row. The component will return the selected lead records as a comma separated list of lead IDs along with the number of rows selected.

Creating the Schedule Email Records

With the list of lead record IDs a loop is built in the flow using a Decision element and the number of rows selected variable. As long as the variable is greater than zero the loop will continue.

To parse CSV ID values, a formula is used take first 18 characters on the left side of the text value. This will result in the first ID in the string. Each extracted ID is used to create a new lead sObject variable where we store the ID in the lead lookup field along with the email details and schedule date and time. Each sObject variable will be added to a sObject collection variable.

After the left most lead ID has been processed, we will remove it from the main LeadIds text variable (including the comma) and the NumOfRecs number variable will be decremented by 1.

Finally, once the loop is complete the Fast Create element is used to create the Scheduled Email records with the sObject collection variable comprised of Scheduled Email sObject variables.

Scheduling the Email

Once the Scheduled Email records have been created a process builder flow will execute and scheduled the email to be sent out based on the scheduled time.

The condition for action group of the flow will be that email message, email subject, and lead lookup field is not blank.

If the above conditions are met, an immediate action is fired to update the Email Address field of the record to be email field value of the related Lead record.

A scheduled action will also be queued 1 hour after the date time value of the Scheduled Time 1 Hour Before formula field which, as the field label states, will be 1 hour before the set Schedule Send Time.

Since process builder does not allow an action to execute on a specific time I used the formula value trick in order to have the email sent out on the actual scheduled date and time.

The scheduled action itself will just execute an email alert I set up that uses an email template. The email alert uses the value stored in the Email Address field as the recipient.

The email template will use the email subject and email message provided during the flow entry process.

Here are screen shots of the finished product.

Enter email details and scheduled time.

Selecting Lead Status for search.

Selecting leads to be included in the scheduled email.

Message indicating emails were scheduled successfully.

With a combination of visual workflow, lightning component, and process builder we were able to build an email scheduler with zero Apex code involved. Hope you enjoyed the read and come back for the next app!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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