Create Your First Flow – Mass Emailing Contacts

Share this article...

In my previous post we discussed what Salesforce Flows are, how to use the Flow Builder, and why you (and everyone who administers a Salesforce Org) should learn how to use them. Now that we’ve learnt the basics, it’s time to dive deeper and start building your first Flow! Let’s follow the business case below, and build a solution using Flows.

If you plan to play along please do so in a Developer Org or Sandbox!

Business Case: Your sales team have recently started using Salesforce to manage their pipeline – and they’re loving it! The only negative feedback you’ve received so far, is that sometimes they want to send a mass email to all Contacts related to a single Account, and having to send emails one by one is wasting precious time.

Solution: You can create a Flow that will be triggered from an Account record and send an email to all the Contacts that are related to that particular Account. Users will simply click the button and the Flow starts – nice and easy!

Step 1: Create a New Flow

First head to Setup > Process Automation > Flows (search for “Flow” in Quick Find). Select “Flows”, then click the “New Flow” button.

Select the “Screen Flow” option and click “Create”.

Next, select “Freeform”.

It’s now time to create your variables. Variables are resources that store data for use inside the Flow.

Step 2: Add Variables

All the variables you’ll need are detailed below. To create a variable, click “New Resource” under the Manager tab in your Toolbox.

recordId: This needs to be a Text Variable that can receive text from outside the Flow (to do this we need to enable Available for Input.

  • Resource Type: Variable
  • API Name: recordId
  • Description: The Id of the Account record that this Flow was called from.
  • Data Type: Text
  • Allow multiple values: False
  • Available for input: True
  • Available for output: False

emailAddresses: This needs to be a Text Variable that can hold multiple values (a collection).

  • Resource Type: Variable
  • API Name: emailAddresses
  • Description: The Email addresses of the Contact records.
  • Data Type: Text
  • Allow multiple values: True
  • Available for input: False
  • Available for output: False

Step 3: Build the Flow

Now that you’ve got your resources built, it’s time to use them in your Flow. The first thing you need to do is create the email itself; we’ll allow the User to input the content through a Screen element. To do this, move over to the Elements tab and drag a Screen element onto the canvas. In the Screen Properties panel, set the Label to “Draft Email”, and press the Tab key on your keyboard to automatically populate the API Name (it should set itself to “Draft_Email”). Set the Description to “Allow the User to write their email.”

Next, you’ll drag a Text component and a Long Text Area component onto the screen canvas. Label them “Subject” and “Body”. Your screen should look like the image below.

Once you’re finished, click “Done” at the bottom of the screen.

We then need to find all the Contacts related to the Account and store them in the Collection Variable that Salesforce will automatically create. To do this, drag a Get Records element onto the canvas and configure it as shown below. Once you’re finished, click “Done”.

Label: Get Contacts

API Name: Get_Contacts

Description: Gets the Contacts related to the Account.

Object: Contact

Conditions: AccountId Equals recordId

How Many Record to Store: All records

How to Store Record Data: Automatically store all fields

In the Manager tab, under Record Collection Variables, you’ll see a new “Contacts from Get_Contacts” collection. Salesforce has created this automatically and tied it to the “Get Record” element you just created. Neat!

In order to send the email to each Contact we’ll need to gather all the email addresses within a single Collection Variable (emailAddresses). You’re going to use a Loop to do this.

In the Elements tab, drag a Loop onto the canvas. Configure it as shown below and click “Done” when you’re finished.

Label: Loop Contacts

API Name: Loop_Contacts

Description: Loops through the Contacts

Collection Variable: {!Get_Contacts}

A Loop allows you to perform an action on individual variables within a collection. For each of the Contacts in the collection, we need to add their email address to the emailAddresses collection we created earlier. To do this, we’ll use an Assign element. Drag one onto the canvas and configure it as follows. When you’re finished, click “Done”.

Label: Add Email Address

API Name: Add_Email_Address

Description: Adds the Email of a Contact to the emailAddresses Collection

Variable: emailAddresses

Operator: Add

Value: Current Item from Loop Loop_Contacts.Email

Finally, now that we have the content of the email and all the recipients ready to go, the last step is to send the email to the recipients. We can do this using an Email Action. Drag an Action element onto the canvas. In the Action field, use Send Email. Configure as below and click “Done”.

Label: Send Email

API Name: Send_Email

Description: Send the email to the Contacts

Body: {!Body}

Subject: {!Subject}

Email Addresses (collection): {!emailAddresses}

Sender Address: {!$User.Email}

Fantastic! All your elements are built and configured; it’s now time to connect it all together.

To do this, grab the circle node from the Start element and drag it to the “Get Contacts” element, and so on (chronologically). When you’re done, it should look like the image below.

NOTE: Don’t forget to drag the Loop’s node to the Assignment element and then the Assignment back to the Loop. This closes the Loop.

If you haven’t already, you should save your Flow at this point. The Save button is at the top-right of the Flow canvas. Don’t activate it just yet!

Take a note of the Flow Label and API Name as you’ll need them in a later step when we create our button.

Step 4: Test the Flow

Before we activate the Flow and grant our Users access to it, we need to make sure it works. We’ll create some test data together first.

Remember, we want it to email multiple Contacts related to a single Account, and no other Contacts.

We’re going to create two test Accounts, and three Contacts.

The first Account is going to be called “Positive Test”. The rest of the details you populate on this Account won’t matter.

Once you’ve created the Account, create two Contact records related to it. You will need to use real email addresses that you have access to and they need to be different for each Contact.

First Name: Test

Last Name: Contact1

Account: Positive Test

Email: any email address you own

First Name: Test

Last Name: Contact2

Account: Positive Test

Email: any email address you own, different to Test Contact1’s

Now that you’ve got your Positive Test, it’s time to create one more Account called “Negative Test” and add a third Contact to that. You’ll need access to the email address once more, and it needs to be different from the first two. The reason we’re creating this one is to make sure it ONLY sends the email to the Contacts related to a single Account, and not to all Contacts.

First Name: Test

Last Name: Contact3

Account: Negative Test

Email: any email address you own, different to Test Contact1 and Test Contact2

Once you’ve got both Positive Test and Negative Test Accounts created, and their respective Contact records, navigate back to Positive Test and copy the Record Id from the URL. We’ll use this to test the Flow. Once you’ve copied it, go back to your Flow tab.

We’re going to use Flow Builder’s debug tool to test the Flow and make sure it functions as expected. The “Debug” button is at the top-right of your page, next to the “Save” button. Make sure your Flow is saved before you continue as it will test the most recently saved version.

Once you’re in the debug screen, you’ll see a window similar to the image below. Paste your Account’s Record Id into the recordId variable box (if there’s no recordId box, you need to go back and ensure you made your recordId variable available for Input). When you’ve pasted it in, click “Run”.

If all was created correctly, you should see Subject and Body text fields. Go ahead and populate these – this will be the content of the test email you send in the next step. When you’re done, hit “Next”.

If all has gone to plan you should see the “All done” screen. Go and check all three email inboxes associated with the test Contacts you created (including Spam folder). The email should have been received by both Contacts related to Positive Test Account, but not to the Contact related to the Negative Test Account. Bravo! Activate your Flow, and it’s time to make it accessible to the Users.

Step 5: Create a Button

We want to give our Users the ability to call this Screen Flow from an Account record. To do this, we’ll create a new Action on the Account Object.

Head to Setup > Object Manager > Account > Buttons, Links, and Actions.

Click “New Action” at the top-right of the page. Configure your Action as follows, and click “Save” when you’re done. This is where you’ll need the name of your Flow that you created before.

Action Type: Flow

Flow: Email All Contacts for Account

Label: Email All Contacts

Name: Email_All_Contacts

One more thing, we need to add the Action to the Page Layout. On the Account object, head over to Page Layouts, then edit the Page Layout (or multiple Page Layouts) that you want to add your Action to.

Drag your “Email All Contacts” Action from the Mobile & Lightning Actions section onto your Page Layout inside the Salesforce Mobile and Lightning Experience Actions section. Click “Save”.

It’s a good idea to test and make sure your button shows up where expected and calls the Flow as expected too.

Summary

Congratulations! You’ve now built your first Flow using Flow builder, and you’ve made it available to your users by means of an Action on the Account Page Layout.

You can play around with customising it if you’d like. You could put a Decision element in before the Send Email element and check to see if there are any values in emailAddresses before sending, you could change the criteria for Get Contacts to ensure it only collects Contacts that have the Email field populated, and you can add another ‘error’ screen that tells the User if there are any issues.

Mastering Flow should be number one on your priorities list if you are a Salesforce Administrator. You’ll be able to configure the org for your Users in a number of powerful ways with no need to write a single line of code.

There are, of course, some best practices relating to Flow that you should follow, but that’s coming in another post.

20 thoughts on “Create Your First Flow – Mass Emailing Contacts

  1. Thanks Ben! I’d like to caution folks to think about allowing people to unsubscribe from emails sent out from Salesforce. Here’s my blog on how to add an unsubscribe link in native emails.

  2. I’m new to this and my flow doesn’t seem to work. I may be missing something in the fast lookup. You didn’t show the bottom half of that screen so I winged it. What is the Variable used, and the record’s fields to save in the variable. I won’t say what I did, I’ll just wait till I get the right answer! Thanks

  3. For three days I was trying to understand the FastLook up, Loop and assignment so that to result in a collection variable in order to use it for emailing. I was almost left bald…

    Thank you so much for this post. Literally with just two screens I understood how to properly utilise the various elements.

    If you ever visit Athens Greece, drinks are on me.

  4. Just finished designing the flow part I was missing. I run the flow without activating it and was prompted to the following Error: The “XXXTestEmail_Action” element in your flow has validation errors.

    I am working from a trailhead org as admin if this helps.

    What can this possibly be and how can I debug it since the email action even if it is connected to the fault email is not sending out anything.

    Thank you for your time.

  5. Ignore above emails. The solution works like a charm. The error observed on my side was due to a forgotten variable (formula) from previous attempts.
    What I learned is that all Errors (that will prohibit the flow from activating) need to be addressed prior to pressing the RUN button the flow otherwise this Error message will pop up. In my case the variable was not even utilised.

    Thank you for all the material you provide to the newcomers. SF would not be what it is if the community was not giving and helpful.

  6. I’m trying to setup a flow that will gather all the Contact Emails that are associated with an account. I think that I can use this example but I need to be able to use it to then send an email template from a process builder. Would I just need to leave out the screen that was created for the Subject and body of the email? Thanks for the help!

  7. Hi Ben,

    When I have added the emails to a collection, I would like to grab the first names and insert them into an email template later with a formula…

    Hi Ben

    or

    Hi Ben and Peter,

    or

    Hi Ben, Peter and Lisa,

    Where do I add a new collection variable for the first names, and how do I create the formula to analyse if there are one or more first names?

    This would be a fantastic add-on to this article.

    Br,

    Henrik

  8. Or you go the Accounts contact related list select all, add to campaign and then send list email allowing you to use lighting templates/send later/ preview/ scheduler etc..

  9. Hi there. I made this flow as I’m semi-new to Flow and thought this would be an easy/useful one to have. The issue I ran into though is that despite me having the restriction of AccountId = recordId, it pushed every contact into the emailaddress collection during the loop.

    Both concept and design look fine to me. I do not understand why it is pulling in everyone. That’s where it is failing (can’t have more than 5 emails – a limit you probably should mention in your post). I just thought I’d ask for your input since it was your flow. 🙂

  10. If you are thinking of using this for real (rather just to get a little experience of flow)

    In your get contacts element you should
    – select “Choose fields and assign variables (advanced)”
    – click in the Record Collection box and create a new resource > Type = Variable > Name: contactEmails > Data Type: Record (and check the Allow Multiple Values box) > Object: Contact
    – back in the Get Records dialog, in the Field box, enter: email and check the “When no records returned, set specified variables to null” box
    – Click Done

    You should also add conditions to the query such as email is not null (for this add condition of email > Is Null > False) and as the article suggests, to ensure that they haven’t opted out of email (Email Opt Out > Equals > False). You may also want to exclude contacts who have bounced emails and are not deleted, haven’t left the company and any other fields your implementation might have which you think would be worth evaluating

    After the get records element and before the loop, add a decision element to check your collection of records isn’t null before executing the email action

    This way if an account has no contacts with emails who haven’t opted etc. out you won’t get an error

    @JeffHughes – without seeing your flow, it’s hard to be definite what the problem is but things that can lead to issues are
    – the name of the recordId variable has to be exactly that as it’s case sensitive and must be available for input
    – most likely since you are getting every contact, the getRecords condition isn’t filtering for the passed in recordId
    – the 5 emails limit is a Developer Edition org limit, so will not be a problem in a sandbox or production

  11. Hello,

    This is a FANTASTIC article. I’ve done some customization like adding a screen with checkbox group to select the contacts our Account Executives would like to send an email to. One thing that I just can’t figure out is, can I somehow get this to send through an External Email Service provider (Outlook, specifically). That’s how we have our emails set up currently. When I send an email through this flow the email received will be “Chris via askdfjakldsjj.salesforce.com”. I’d just like to clean it up.

  12. I facing some issue with the example it’s work fine when we have emil addresses for contact but when we don’t have emil address in all contact emil field for this scenario it won’t work it give an error so how we can handle this situation

  13. We can’t send more than 5 emails currently in the flow action so this just doesn’t fit the purpose of mass email. I thought this was a cool idea to expose these actions on a list view to allows user send mass emails from any object but unfortunately it just hit the limit.

  14. Hi Tim,

    My requirement is similar to this, but I also should allow user to choose the template they want. Then I need to use that template to send email to the selected contacts. How should I allow user to choose template from the screen and pass it to send email (Action)?

  15. Hi there! any suggestions for populated the RelatedToId on the Email Message? I’ve tried to look up the email after the “send email” step and populate the RelatedToId field, but apparently it is only available when Email Message Status is “Draft”.

    Thanks for this fantastic tutorial.

  16. I had a problem with this Flow in the Assignment Element. The problem was the email is type ’email’ in my org. I could not pass this to a text variable . Solved by creating a new formual field called ConEmail that was type Text.

Add Comment