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
- iPhone 13 Plus – Quantity (3) – Sales Price ($0)
Prerequisite
- First, create two Price books Manufacturing and Retail.
- Add a few products to the Manufacturing price book as shown in the following screenshot:
- 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):
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:
- Define flow properties for record-triggered flow
- Formula to determine whether the opportunity is created or updated
- Add a decision element to check the associated pricebook on the opportunity
- Add a Get Records element to find the pricebook entry for iPhone 13 Plus
- Add a decision element to check if the price book entry for iPhone 13 Plus was found or not
- Add a create records element to add a product to the opportunity
Step 1: Define Flow Properties
- Click Setup.
- In the Quick Find box, type Flows.
- Select Flows then click on the New Flow.
- Select the Record-Triggered Flow option, click on Create and configure the flow as follows:
- Object: Opportunity
- Trigger the Flow When: A record is created or updated
- Set Entry Criteria
- Condition Requirements: None
- Optimize the Flow For Action and Related Records
- Click Done.
Step 2: Formula to Determine Whether the User is Created or Updated
- Under Toolbox, select Manager, then click New Resource to determine whether the record is created or updated.
- Input the following information:
- Resource Type: Formula
- API Name: forB_IsNew
- Data Type: Boolean
- Formula: IsNew()
- 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.
- On Flow Designer, click on the +icon and select the Decision element.
- Enter a name in the Label field; the API Name will auto-populate.
- Under Outcome Details, enter the Label the API Name will auto-populate.
- Condition Requirements to Execute Outcome: All Conditions Are Met (AND)
- Row 1:
- Resource: {!forB_IsNew}
- Operator: Equals
- Value: {!$GlobalConstant.True}
- Click Add Condition
- Row 2:
- Resource: {!$Record.PriceBook2Id}
- Operator: Is Null
- Value: {!$GlobalConstant.False}
- Click Add Condition
- Row 3:
- Resource: {!$Record.PriceBook2.Name}
- Operator: Equals
- Value: Manufacturing
- Row 1:
- When to Execute Outcome: If the condition requirements are met.
- 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.
- On Flow Designer, below the Manufacturing node, click on the +icon and select the Get Records element.
- Enter a name in the Label field; the API Name will auto-populate.
- Select the Price Book Entry object from the dropdown list.
- Select All Conditions Are Met (AND).
- Set Filter Conditions
- Row 1:
- Field: Pricebook2Id
- Operator: Equals
- Value: {!$Record.PriceBook2Id}
- Click Add Condition
- Row 2:
- Field: Name
- Operator: Equals
- Value: iPhone 13 Plus
- Row 1:
- How Many Records to Store:
- select Only the first record
- How to Store Record Data:
- Choose the option to Automatically store all fields.
- 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.
- On Flow Designer, click on the +icon and select the Decision element.
- Enter a name in the Label field; the API Name will auto-populate.
- Under Outcome Details, enter the Label the API Name will auto-populate.
- Condition Requirements to Execute Outcome: All Conditions Are Met (AND)
- Row 1:
- Resource: {!Find_Pricebook_Entry}
- Operator: Is Null
- Value: {!$GlobalConstant.False}
- Row 1:
- When to Execute Outcome: If the condition requirements are met.
- 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.
- On Flow Designer, below the Yes node, click on the +icon and select the Create Records element.
- Enter a name in the Label field; the API Name will auto-populate.
- Input the following information:
- How Many Records to Create: One
- How to Set the Record Fields: Use separate resources, and literal values
- Object: Opportunity Product
- Set Field Values for the User Package License
- Row 1:
- Field: OpportunityId
- Value: {!$Record.Id}
- Click Add Field
- Row 2:
- Field: Quantity
- Value: 3
- Click Add Field
- Row 3:
- Field: PricebookEntryId
- Value: {!Find_Pricebook_Entry.Id}
- Click Add Field
- Row 4:
- Field: UnitPrice
- Value: 0
- Click Done.
In the end, Martin’s Flow will look like the following screenshot:
Once everything looks good, perform the steps below:
- Click Save.
- Enter Flow Label the API Name will auto-populate.
- Click Show Advanced.
- API Version for Running the Flow: 54
- Interview Label: Auto Add Product to Opportunity {!$Flow.CurrentDateTime}
- 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.
- Navigate to the Opportunity tab, and create a new opportunity for the Manufacturing record type.
- 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.
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.
This can be achieved via a before-save flow on the opportunity and a custom field. Let me know if you’re still looking for a solution.
my flow is not working no error are coming in flow but product did not created
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!
What about using record-triggered flow? Pls, share the process builder screenshots with me.
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!
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
I was looking for this! Perfect!
Hello! Is this something that can be used on the Orders object?
Yes you can – make sure to replace object and fields
Hi Rakesh,
This for some reason is not working for me. I might need your help…yet, again!
Send me an email here info at automationchampion.com – error email
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
I’ve used custom Label not name. Thank you!
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
Then in such case, use Record Lookup elements to find the desired IDs
This worked perfect!!! Thank you so much for expanding on this use case for us, so much appreciated. Onwards, MB.
🙂