This has been a very common question on the Answers Community for the last few years. How do you send an Email to a Primary Contact on the Contact Roles related list of an Opportunity? Before the era of Process Builder and Flows, writing an Apex trigger was the only way out. The Apex trigger would query the related Opportunity Contact Roles, find the Primary Contact, grab his/her Email ID and then send an Email to the recipient. Now when we have powerful tools like Process Builder and Visual Workflows, the need for Apex trigger has been eliminated. Let me walk you through how simple and convenient it is to implement the following requirement:
Requirement: The system administrator needs to create an automated process which sends an Email to the Primary Contact on the Contact Roles related list of an Opportunity when this Opportunity is set to Closed Won. The users would also like to see this Primary Contact as a lookup field on the Opportunity so that it can be used to bring down more information from the related Primary Contact via formula fields.
Design Approach (questions to ask yourself before you begin the implementation):
- What are we really trying to do here?
So there are two parts to this implementation. First one queries the Contact Roles related list, finds the Primary Contact, grabs the E-mail and ContactId and pushes them to the related Opportunity. We would need two custom fields on the Opportunity, one as a lookup field to store the Primary Contact and the other as an Email field to store the Primary Contact’s Email Id. The second part is straightforward automation which sends an Email to the Primary Contact using the populated Email field.
- What should be the trigger for the automation?
We want the Opportunity to get updated with the Primary Contact info every time it’s created or edited. This becomes the evaluation criteria.
- Which automation tool/(s) should we use to implement this requirement?
Since we need to query the Contact Roles related list, Visual Workflow is our only option here. And because we want this Flow to launch when an Opportunity is created or edited, a Process comes into the picture as well. For sending the final E-mail, we can either use a Workflow Rule or Process but for consistency, let’s stick to a Process.
Based on what we have laid out so far, we first create a Flow which accepts the Opportunity Id from a Process. The Process is created on the Opportunity object and fires every time the Opportunity is created or edited. The Flow performs the heavy lifting of finding the Primary Contact within the Contact Roles related list. It then grabs the Email Id and Contact Id of this Contact and updates the related Opportunity fields. We also create a Process which fires an Email using the populated Email field as soon as the Opportunity stage is set to Closed Won. This is what the complete automation flow looks like:
Now when we have brainstormed the requirement, let’s get down to business.
CREATING THE FLOW:
We need three variables in this Flow:
- varOppId for receiving the Opportunity Id from the Process (Text type)
- varEmail for storing the Primary Contact’s Email (Text type)
- varPrimaryContactId for storing the Primary Contact’s Salesforce Id (Text type)
Using the Record Lookup element: We query the Opportunity Contact Roles object to find the Primary Contact associated with our Opportunity. We then store the found Contact’s Id in the varPrimaryContactId variable.
Tip: Performing a Null check even when it doesn’t seem indispensable is always considered a good practice.
Using another Record Lookup element: This element finds the Contact with the ID value stored in varPrimaryContactId variable and then stores its Email in a varEmail variable.
Using the Record Update element: This is the most important element of our Flow. It updates the two custom fields on the Opportunity with the variable values: Email and Contact Id. These fields can then be used to perform further automation like sending email alerts to Primary Contact or pulling down other miscellaneous from the Primary Contact record onto the Opportunity record.
CREATING OUR PROCESS THAT LAUNCHES THE FLOW:
Don’t see your Flow in the list when trying to set it an Immediate Action above? Go check if it’s activated. Activate the Process as well and we are good to test now!
AFTER the magic happens (edit and save the Opportunity record):
Wasn’t that simple? You can now build a Process that sends an Email Alert to the above Primary Contact when the Opportunity Stage is set to Closed Won. If you have reached this far, I am sure you can build this straightforward automation on your own. Moreover, you can bring in field values like Contact Address, Contact Phone, Contact Owner etc. onto the related Opportunity, as needed. Feel free to share in the comments below any similar scenario involving Opportunity Contact Roles and the automation you performed without using code.
Related Answer Community/Stackexchange questions (and Idea):