Need to Auto Add Opportunity Contact Role to an Opportunity? Read on!

Need to Auto Add Opportunity Contact Role to an Opportunity? Read on!

Last Updated on April 16, 2022 by Rakesh Gupta

Big Idea or Enduring Question:

How do you automatically add an Opportunity Contact Role to an Opportunity? 

Objectives:

After reading this blog, you’ll be able to: 

  • Understand opportunity contact role
  • Avoid hard code of Id using the get records element 
  • Use decision element to check record values
  • Auto-create a record using Salesforce Flow

Business Use case

Corey Eridon is working as a System Administrator at Gurukul on Cloud (GoC). He is in the process of implementing Opportunity management at GoC. Last week, he received the following request from the Pardot marketing team:

  • Whenever Opportunities are created for Partner Account, automatically add contacts, with Chief Executive officer in their Title, as a primary contact role on the Opportunities.

What is an Opportunity Contact Role?

Opportunity Contact Role has two aspects to it: (1) it represents the role a Contact play on an Opportunity; and, (2) it is a junction object between Opportunity and Contact. As we all know, Opportunity and Contact share a many-to-many relationship. For example, a Contact can play a Decision maker role in one Opportunity; and, an Executive sponsor role in another opportunity.

The challenge arises when one wants to configure automation around the Opportunity object and Contact Roles. For example, let’s say that – (1) whenever someone creates an opportunity for an Account, you want to auto add all contacts as contact roles; or, (2) whenever someone creates an opportunity for an Account, you want to auto add key contacts as contact role for the Opportunity.

Automation Champion Approach (I-do):

To solve this requirement, we will use the After-save Record-Triggered Flow. Check out this article to understand why we are using after-save record-triggered flow for this scenario. 

Before diving further, let me also show you a diagram of the Flow at a high level. Please spend a few minutes going through the following Flow diagram and understand it. Let’s begin building this automation process.

Guided Practice (We-do):

There are 6 steps to solve Corey’s business requirement using After-Save Record-Triggered Flow. We must:

  1. Define Flow properties for record-triggered flow
  2. Create a formula to determine whether the opportunity is created or updated
  3. Add a decision element to check if the opportunity’s account is a partner account
  4. Add a get records element to find the contact with the title chief executive officer on account 
  5. Add a decision element to check if contact was found or not 
  6. Add a create records element to associate a contact to an opportunity as a primary contact role

Step 1: Define Flow Properties

  1. Click Setup.
  2. In the Quick Find box, type Flows.
  3. Select Flows then click on the New Flow.
  4. Select the Record-Triggered Flow option, click on Create and configure the flow as follows:
    1. Object: Opportunity
    2. Trigger the Flow When: A record is created or updated
    3. Set Entry Criteria
      1. Condition Requirements: None
    4. Optimize the Flow For Action and Related Records
  5. Click Done.

Step 2: Formula to Determine Whether the Opportunity is Created or Updated

  1. Under Toolbox, select Manager, then click New Resource to determine whether the record is created or updated. 
  2. Input the following information:
    1. Resource Type: Formula
    2. API Name: forB_IsNew
    3. Data Type: Boolean
    4. Formula: IsNew()
  3. Click Done.

Step 3: Using Decision Element to Check if the Opportunity’s Account is a Partner Account

Now we will use the Decision element to check if the opportunity’s account is a partner account. 

  1. On Flow Designer, click on the +icon and select the Decision element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Under Outcome Details, enter the Label the API Name will auto-populate.
  4. Condition Requirements to Execute Outcome: All Conditions Are Met (AND)
    1. Row 1:
      1. Resource: {!$Record.AccountId}
      2. Operator: In Null
      3. Value: {!$GlobalConstant.False}
    2. Click Add Condition
    3. Row 2:
      1. Resource: {!$Record.Account.IsPartner}
      2. Operator: Equals
      3. Value: {!$GlobalConstant.True}
    4. Click Add Condition
    5. Row 3:
      1. Resource: {!forB_IsNew}
      2. Operator: Equals
      3. Value: {!$GlobalConstant.True}
  5. When to Execute Outcome: If the condition requirements are met
  6. Click Done.

Step 4: Adding a Get Record Element to Find a Contact with Title Chief Executive Officer 

The next step is to use the Get Records element to find contact with Title Chief Executive Officer.

  1. On Flow Designer, below the Partner Account node, click on the +icon and select the Get Records element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Select the Contact object from the dropdown list.
  4. Select All Conditions Are Met (AND)
  5. Set Filter Conditions
    1. Row 1:
      1. Field: Title
      2. Operator: Equals
      3. Value: Chief Executive Officer
    2. Click Add Condition
    3. Row 2:
      1. Field: AccountId
      2. Operator: Equals
      3. Value: {!$Record.AccountId}
  6. How Many Records to Store:
    1. select Only the first record
  7. How to Store Record Data:
    1. Choose the option to Automatically store all fields
  8. Click Done.

Step 5: Using Decision Element to Check If Contact was Found or Not 

Now, will use the Decision element to check if the previous Get Records element returns a contact record. 

  1. On Flow Designer, click on the +icon and select the Decision element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Under Outcome Details, enter the Label the API Name will auto-populate.
  4. Condition Requirements to Execute OutcomeAll Conditions Are Met (AND)
    1. Row 1:
      1. Resource: {!Get_Contact}
      2. Operator: Is Null 
      3. Value: {!$GlobalConstant.False}
  5. When to Execute Outcome: If the condition requirements are met.
  6. Click Done.

Step 6: Add Action – Create a Record

 The next step is to create a record for the Opportunity Contact Role object, for this, we will use the Create Records element.

  1. On Flow Designer, below the Yes node, click on the +icon and select the Create Records element.
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Input the following information:
    1. Enter Label the API Name will auto-populate.
    2. How Many Records to Create: One
    3. How to Set the Record Fields: Use separate resources, and literal values
    4. Object: Opportunity Contact Role
    5. Set Field Values for the Opportunity Contact Role
    6. Row 1:
      1. Field: ContactId
      2. Value: {!$Get_Contact.Id}
    7. Click Add Field
    8. Row 2:
      1. Field: OpportunityId
      2. Value: {!$Record.Id}
    9. Click Add Field
    10. Row 3:
      1. Field: IsPrimary
      2. Value: {!$GlobalConstant.True}
  4. Click Done.

In the end, Corey’s Flow will look like the following screenshot:

Once everything looks good, perform the steps below: 

  1. Click Save.
  2. Enter Flow Label the API Name will auto-populate.
  3. Click Show Advanced.
  4. API Version for Running the Flow: 55
  5. Interview Label: Auto Add Opportunity Contact Role {!$Flow.CurrentDateTime}
  6. Click Save

Almost there! Once everything looks good, click the Activate button.  

Proof of Concept

From now onwards, if a business user creates an Opportunity for a Partner account, then, the Record-triggered flow will automatically add a contact with Title Cheif Executive Officer as Opportunity Contact Role to it.

  1. Navigate to the Opportunity tab, and create a new opportunity for the Partner account.
  2. Once done, click on the Save button. The process will automatically add a contact role with a contact whose title is Cheif Executive Officer, as shown in the following screenshot:Blog 95 - OUtcome

Formative Assessment:

I want to hear from you!

What is one thing you learned from this post? How do you envision applying this new knowledge in the real world? Feel free to share in the comments below.

Have feedback, suggestions for posts, or need more information about Salesforce online training offered by me? Say hello, and leave a message!

Leave a Reply

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