Lightning Flow: Create a Button to Send Auto-Generated Email
Big part of being an administrator is improving users’ experience. This often means customizing processes to boost productivity. Today, we’re going to look at how we can automate one of the most common tasks sales representatives do on a daily basis. Sending emails.While Salesforce allows sales reps to send emails with the Activity object, sometimes your users require a more streamlined process. For example, when sending a link, or a promo code. Making it an automated process is a good way to eliminate errors, and can greatly reduce time that would be spent on manually personalizing each message.To lift the burden off their shoulders, we’ll create a flow, launched from a button, that sales reps can use to send automatically generated emails.
Create the Flow
Without further ado, let’s get to work. We will start by creating a lightning flow.Go to Setup and enter Flows in the search box.Click “New Flow” in the upper right hand corner. Choose a “Screen Flow” type of flow. You can pick Freeform or Auto-Layout. It’s a matter of personal preference.As the first step in our flow we need to gather information from a record we are launching. For this, we’ll create a special type of variable that’s going to store the Id of the record that started the flow. If you don't know how variables work, look at this Trailhead module.Click “New Resource” in the “Manager” tab.Resource Type: VariableAPI Name: recordIdData Type: TextAvailable for input: Checked
Now, drag and drop the “Get Records” element onto the canvas. This element is used to look for a specific record, or records. In our case, we’re looking for a lead record that started our process. Let’s configure it accordingly.Get records of this object: LeadFilter lead record: “Id” equals “recordId” (our variable)
As you can see, we’re trying to find a record with Id that is equal to the Id stored in our variable.The second step of configuring the “Get Records” element is to decide which field values we want to save to use later. We can use the simplest method, and store all fields automatically. Lightning Flow will do everything for us.What would be the reason to pick the third option and manually assign each field to a variable? The manual assignment allows us to set specified variables to null if no records are returned. If there is a possibility that the “Get Records” element won’t find any records in your automated process, make sure to do it manually to avoid a fatal error that could break your flow.The next element we’ll drag onto the canvas is going to be a “Screen”. We will use it to show a message to the end-user. Use the “Display Text” component to write a short text. You can also use a variable to display details from a record.
Also, remember to uncheck the options for displaying the “Previous” and “Pause” buttons. They aren’t necessary.Now, for the most important part of the flow. After the user hits “Next”, our Flow will send a message to the email specified on the Lead record. For this, we are going to use an Action - another element available in the left-hand menu. Drag and drop it into the canvas.Find “Send Email” core action using the search box.The “Set Input Values” is the section where we configure our message. The first field “Body” is the email body. Click on it and pick +New Resource. This time, look for the “Text Template” type of resource (although this parameter accepts single-value resources of any type).I recommend switching to the Plain Text mode. “Text Template” has been known for bugs and Rich Text mode may break your message, or add markup tags that you don’t want your customers to see.Also, this kind of flow is best used for messages containing some sort of generated code, or a link, so using variables here can be very useful. And, of course, don’t forget to personalize your message by using “First Name” or other fields from the object you are building this flow for.For the “Subject”, you don’t really need a variable. A simple phrase will suffice.And now, for the rest of the fields:Email Address (collection) - If a part of your flow is a loop collecting email addresses from several records, and you want to use them as recipients, this is the place to assign your collection variable.Email Addresses (comma-separated) - Include this if you want to send emails to more than one recipient, but you don’t want to use a collection variable.Sender Address - Required if you are planning to use an organization-wide email address. In that case, fill it out with the right email address.Sender Type - An optional field. It determines who’s the sender of the email. The available options are:
- CurrentUser - email address of the running user will be used (default).
- DefaultWorkflowUser - email address of the default workflow user
- OrgWideEmailAddress - use it when you want to use an organization-wide email address as the sender address.
At this point, our automation did what it was supposed to, so let’s inform the end-user that the email got sent. Let’s create another screen. This time, we’ll include a simple confirmation message.
Connect the elements on the canvas, save the flow and activate it.
And that’s it. Our flow is ready.Before going to the next step I do recommend using the “Debug” tool to test your flow. Because we have a variable available for input, the debug tool will ask you for a text input. You can create a test lead, and use its Id. This way you’ll be able to identify, and fix any possible errors.Even without testing it out, you might already be aware of one of the possible issues. What if the lead that started the flow has an empty Email field? I’d suggest either creating an alternative path in the flow (the recommended approach), or making the Email Address a required field.If you decided to use a variable in your email body, make sure that a similar issue wouldn't happen there.
Create a Button
The first phase of our project is done. Now, let’s see how we can add this newly-created flow to a Lead page.Go to Object Manager -> Lead -> Buttons, Links and Actions, and create a new Action.Action Type: Flow (choose the flow you’ve just created)
Use a custom label, and change the icon if you want to. Now, go to page layouts and edit the one where you want to add the button. You can find it in “Mobile & Lightning Actions”, so just drag and drop it into the “Salesforce Mobile and Lightning Experience Actions” section. You might need to click the wrench icon in the right corner, if you’ve never modified it before.
And that’s it.Before you roll this out to your users you should check if they have the permission to run flows. Also, make sure that only people who should be able to access the button are assigned to that page layout.
Remember that the best way to learn is to practise. Don’t hesitate to customize this flow however you like. Try changing the object, or maybe add details from a related record to the email body.Flow is a great tool to streamline processes in your org. If you want to learn more about it, we’ve got other articles on our blog: