Automatically Add Products to New Opportunity

Automatically Add Products to New Opportunity

Last Updated on May 8, 2022 by Rakesh Gupta

Big Idea or Enduring Question:

How do you automatically add product(s) to a new opportunity? 

Objectives:

This blog post will help us to understand the following
  • Using salesforce flow builder to auto-assign products to an opportunity  
  • Use get elements to avoid hard coding of Id
  • Works with pricebook and pricebook entry 
  • and much more

Business Use case

Martin Jones is working as a System Administrator at Gurukul on Cloud (GoC) He has created a process using record-triggered flow to automatically assign Pricebook to new opportunity based on record types, as mentioned below: 
  • If the Opportunity record type is Manufacturing, then assign a price book Manufacturing
  • If the Opportunity record type is Retail, then assign a price book Retail
It’s working well, and Sales reps are happy. However, Martin received another requirement from the VP of Sales when Manufacturing Price Book is added to a newly created opportunity, automatically add the following products to it:
  • iPhone 13 Plus – Quantity (3) – Sales Price ($0)

Prerequisite

  1. First, create two Price books Manufacturing and Retail.
  2. Add a few products to the Manufacturing price book as shown in the following screenshot:
  3. Please go through this article first and make sure to complete the listed steps: Automatically assign Pricebook based on Opportunity Record Type

Automation Champion Approach (I-do):

Once Sales Reps create a new Opportunity, the next step is to add the Price book and products to it. Products are physical goods or services that you sell to customers. Your Sales reps can use products to generate sales quotes, contracts, or orders.
 
Price Book is a catalog in which the regular selling price of products is listed. Price books are used for selling products at different prices based on geography, volume, government contracts, not-for-profit clients, strategic clients, etc. In Salesforce, one product can be associated with multiple Price Books, and one Price Book can have multiple products.
 
Yes! You are right! This calls for a Junction object. And, lo and behold, we have one known as Price Book Entry! Price Book Entry is a junction object between Product and Price Book, as shown in the following screenshot: 
 
This blog is a sequel to my previous blog – Automatically assign Pricebook based on Opportunity Record Type
 
In the previous blog, I discussed how to automatically assign price book based on the Opportunity record type. I received tons of requests to write another blog about how to add a few default products to the new opportunity.

So, here it is! The requirement is interesting. So, we will create a solution for the existing requirement – using Salesforce Flow! 
 
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 discussing the solution, let me show you a diagram of a Process 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 2 steps to solve Martin’s business requirement using Record-Triggered Flow. We must:

  1. Define flow properties for record-triggered flow
  2. Formula to determine whether the opportunity is created or updated
  3. Add a decision element to check the associated pricebook on the opportunity 
  4. Add a Get Records element to find the pricebook entry for iPhone 13 Plus
  5. Add a decision element to check if the price book entry for iPhone 13 Plus was found or not 
  6. Add a create records element to add a product to the opportunity 

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 User 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 Associated PriceBook on Opportunity 

Now we will use the Decision element to check if the Manufacturing pricebook is associated with an opportunity.

  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: {!forB_IsNew}
      2. Operator: Equals
      3. Value: {!$GlobalConstant.True}
    2. Click Add Condition
    3. Row 2:
      1. Resource: {!$Record.PriceBook2Id}
      2. Operator: Is Null
      3. Value: {!$GlobalConstant.False}
    4. Click Add Condition
    5. Row 3:
      1. Resource: {!$Record.PriceBook2.Name}
      2. Operator: Equals
      3. Value: Manufacturing
  5. When to Execute Outcome: If the condition requirements are met.
  6. Click Done.

Step 4: Adding a Get Record Element to Find iPhone 13 Plus Pricebook Entry Id

The next step is to use the Get Records element to find the iPhone 13 Plus pricebook entry Id. 

  1. On Flow Designer, below the Manufacturing 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 Price Book Entry object from the dropdown list.
  4. Select All Conditions Are Met (AND)
  5. Set Filter Conditions
    1. Row 1:
      1. Field: Pricebook2Id
      2. Operator: Equals
      3. Value: {!$Record.PriceBook2Id}
    2. Click Add Condition
    3. Row 2:
      1. Field: Name
      2. Operator: Equals
      3. Value: iPhone 13 Plus
  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 iPhone 13 Plus Pricebook Entry was Found or Not 

Now we will use the Decision element to check the Record Variable from step 4 to find if it returns the pricebook entry Id or not. 

  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: {!Find_Pricebook_Entry}
      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 Create Records Element to Add Product to Opportunity 

The final step is to add iPhone 13 Plus product to the opportunity. For this, we will use 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. How Many Records to Create: One
    2. How to Set the Record Fields: Use separate resources, and literal values
    3. Object: Opportunity Product 
    4. Set Field Values for the User Package License
    5. Row 1:
      1. Field: OpportunityId
      2. Value: {!$Record.Id}
    6. Click Add Field
    7. Row 2:
      1. Field: Quantity
      2. Value: 3
    8. Click Add Field
    9. Row 3:
      1. Field: PricebookEntryId
      2. Value: {!Find_Pricebook_Entry.Id}
    10. Click Add Field
    11. Row 4:
      1. Field: UnitPrice
      2. Value: 0
  4. Click Done.

In the end, Martin’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: 54
  5. Interview Label: Auto Add Product to Opportunity {!$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 with record type Manufacturing, then, then Salesforce Flow will automatically add price book and products to it.

  1. Navigate to the Opportunity tab, and create a new opportunity for the Manufacturing record type.
  2. Once done, click on the Save button. The flow will automatically add the Price book and default Products to it, as shown in the following screenshot:

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!
Preferred Timing(required)

22 thoughts on “Automatically Add Products to New Opportunity

  1. Hi Rakesh,
    Little more complex use case than your example. Any idea if a “screen flow” could be used to create an Opportunity, create quote with product and quantity, then close/won Opportunity and make quote “Ordered” all automatically for user via screen flow with couple visual questions like asking for selection of account for Opportunity, ask for Oppotunity name and select product and quantity?

    1. Yes, it is possible to create a wizard using Screen Flow to create an opportunity, add products, and create a quote (you have to use Apex if you want to auto-generated Quote PDF).

  2. This is exactly what I needed to build – unfortunately it appears to work in Debug mode (Create Records message is “A record is ready to be created when the next screen, pause, or location action is executed or when the interview finishes”) an of course it runs in rollback mode so my test opportunity is not updated. However, when I activate and create or edit an opportunity record, nothing happens. Ideas?

  3. Lead has record types (lets say Sales and Support) and the same record type is there for Opportunity too. When a lead is converted to Opportunity, based on the lead record type, Opportunity record type need to be set. For example, when a Sales lead is converted, it should create only Sales Opportunity.I was to do this scenario using flow.

  4. Hi Rakesh,
    I have made a process builder where I am able to auto populate the 2 products that I need to put into a new opportunity. I have done the list that you have here and nothing happens for me. I also do not get any errors as well when I make a new opportunity. Do I really need to have 2 different prices books if I am only using the one? And if so, how am I able to do that? Thank you!

  5. We have something similar built to auto create the renewals from closed won opps and bring over related fields and products. The issue we haven’t solved for is the quantity field where we want to bring over whatever the quantity listed was instead of just the default of 1 or whatever number we put in. Any chance you’ve covered this in a help article on flows? I’ve been looking but everything I see is pre-2018 and just sets default field values instead of cloning what was listed…thanks!

  6. Unfortunately get message “versions 3.0 and higher must specify pricebook entry id, others must specify product id”.
    Any suggestions of how tro avoid having to harcode the PBE ID into the process

        1. just because in Product ID You could have given Name while this is ID so please give Product ID

          in Criteria Add iPhone 7

          Field* Type* Value*
          Product ID ID 01t2w000000hU4M

  7. This work Perfect when we execute the apex code manually. But If I want Automatic to extract the PriceBookEntryID for Product and assign it to the Opportunity. Because I have name of Product and PriceBook. And using that I want to add that product to Opportunity

Leave a Reply

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

Discover more from Automation Champion

Subscribe now to keep reading and get access to the full archive.

Continue reading