How to: Add dynamic recipients to an email

Workflows are great when you want to configure sending an email, but there are limitations to it. Chief among them, the only way to include recipients is by specifying the actual User/Contact/Account record, or some lookup field. There are lots of cases where you want to email a list of people based on some action, like “When an invoice is created for an account, send an email to any related contacts with the role {…}”. The scenario I’m going to walk you through today is a little more upbeat: “When an Opportunity is Won, email the sales team to congratulate them and CC their managers.”

Including a query of records on an email is really 3 parts – create the email, attach the recipients, send the email. They need to be separate steps because the out of the box “Send Email” activity will create the email and send it, but there’s no opportunity to modify the email after that. Since we’re breaking up the steps to update the email, we can even update it multiple times (one to include the To field, another for CC, etc).

Querying for the sales team

The first step we need to take is to write a query for who to include in the email. The query can be for any type of record that can be emailed (Contact, Account, User, etc). This particular query if for the sales team for an opportunity, so I’m querying on Users, liked to Connections (with a filter on just the sales roles), then liked to Opportunity (and specifying Opportunity Contains Data). This last criteria indicates the query is for a specific opportunity and you can see all the details at How to: Limit query results to related records. I’m saving this query as ‘Users for this Opportunity’.

emailquery_1_advancedfind

Create the workflow

In this scenario, I’m going to create a workflow called “Congratulate Sales Team” that runs on Opportunity.

emailquery_2_workflowcreate.png

Workflow setup

For this scenario, I’m going to set up the workflow Organization wide and run when the status of the opportunity changes. The first step in my workflow logic is a check condition to ensure the opportunity is Won. If it is, I want to use a CREATE activity yo create a new email record. The Send Email activity will not work in this scenario because the email will be sent immediately. Give the email activity a descriptive name (‘Create email for Team’) and click Set Properties to fill in all the details.

emailquery_3_createemail

emailquery_4_emailcontent.png

You’ll notice I didn’t include any recipients yet – they’ll be added in the next step. Now, if you had a scenario like this except you want to always include Bob and everyone on the team, you can add Bob and any new recipients will be added to the list.

Add recipients to the email

Now that the email exists, add a step for ‘Add Email Recipients from Query’ (“Emails – Add Recipients from Query” with v0.9.4) and click Set Properties.

emailquery_5_customstep.png

On the properties screen, there are three required fields: The email to update, the field to update, and the query of records to add.

emailquery_6_activityproperties.png

In the Email to Update field, you want to select the email you just created. To do that, click in the Value column for Email to Update, then change the drop down under ‘Look for:’ and find the name of the activity you used to create the email record (‘Create email for Team’ in my case). When you select that, select Email Message in the next drop down, hit Add and then Okay. The next field is where you want the recipients add to (To, CC or BCC), and finally you pick a query of records to include. Since I create a personal view earlier, I’m going to select that (‘Users for this Opportunity’).

Send the email

If you leave the workflow as it is you’ll end up with a perfect email that’ll be stuck in Draft status. The final step is to add another activity ‘Send the Email’ (“Emails – Send a Draft Email” with v0.9.4). Click properties again, fill in the email to send the same as before, save and exit.

emailquery_7_sendemailactivity.png

emailquery_8_sendemaildetails.png

See the results

After following the steps above, the next time you win an opportunity your sales team will get a very happy email. Enjoy!

emailquery_9_emailcomplete.png