Auto-generate Renewal Opportunity With Line Items

Auto-generate Renewal Opportunity With Line Items

Last Updated on July 31, 2023 by Rakesh Gupta

Big Idea or Enduring Question:

How do you automatically create a new Opportunity by cloning an existing opportunity with Opportunity Line Items?

Opportunities are the sales and pending deals that you want to track. By adding opportunities, we are building a pipeline, which contributes to sales forecasts. Products are a catalog of items that your company can sell. An Opportunity Product is a specific item or service that is purchased and delivered to the client as it relates to that specific opportunity.

The Opportunity Line Item is also known as Opportunity Product in the user interface. Many organizations are using the opportunity for renewal business of assets, and software. There are no out of box features that allow you to auto-generate renewal opportunities based on the close date of a current opportunity or once an opportunity is successfully closed-won.

Objectives:

After reading this article, the reader will be able to:

  • Using Salesforce Flow to auto-generate renewal Opportunity
  • How to use Get Record element to find the line items from old record
  • How to use a Decision element to find – record variable or record collection variable contains a record or not
  • How to work with Loop elements to create multiple records (or clone records) 

Business Use case

Steven Greene is working as a System administrator at Universal Containers (UC). He received a requirement from the management to auto-create/generate a new opportunity with opportunity line items once an opportunity is closed won. Use the following logic to set the close date of the renewal opportunity

  • Close Date (Renewal opportunity) = Close date (Closed won opportunity) + 365
  • Set Product Unit Price = 0

Automation Champion Approach (I-do):

There are a few possible solutions for the above business scenario, but I’ll use Salesforce Flow to solve the business requirement. Before proceeding ahead, you have to understand OpportunityLineItem objects in Salesforce

  • OpportunityLineItem represents an opportunity line item, which is a member of the list of Product2 products associated with an Opportunity. 

Before discussing it, let me show you a diagram of a Process Flow at a high level. Please spend a few minutes to go through the following Flow diagram and understand it.

Let’s begin building this automation process.

Guided Practice (We-do):

There are 12 steps to solve Steven’s business requirement using Salesforce Flow. We must:

  1. Define flow properties for record-triggered flow
  2. Create a Formula field to generate a close date for a renewal opportunity 
  3. Add a Record variable to store the opportunity line item
  4. Add a Record collection variables to store opportunity line items
  5. Add a Decision element to validate the opportunity Stage equals Closed Won
  6. Add a Create Records element to generate a renewal opportunity
  7. Add a Get Records element to find line items for the old opportunity 
  8. Add a decision element to check if opportunity line item record(s) exists 
  9. Add a Loop element to extract records from the collection variable (From step 7)
  10. Add an Assignment to populate OpportunityId to the opportunity line item record variable
  11. Add an Assignment element to add all line items into a record collection variable  (Created in step 4)
  12. Add a Create Records element to insert opportunity line Items to renewal opportunity (Bulk safe)

Step 1: Salesforce Flow – 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, and 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.

Tips: Never try to write entry criteria in a Record-Triggered Flow. Why? Check out this article.

Step 2: Salesforce Flow – Create a Formula to Generate Close Date For a Renewal Opportunity 

  1. Under Manager click New Resource to create a formula for renewal opportunity close date.
  2. Input the following information: 
    1. Resource Type: Formula
    2. API Name: forD_RenewalOpportunityCloseDate
    3. Data Type: Date
    4. Formula: {!$Record.CloseDate} + 365
  3. Click Done.

Step 3: Salesforce Flow – Add a Record Variable to Store Opportunity Line item Record

  1. Under Manager, click New Resource to create a variable to store opportunity line item records.
  2. Input the following information
    1. Resource Type: Variable
    2. API Name: varR_OpportunityLineItem
    3. Data Type: Record
    4. Object: OpportunityLineItem
    5. Check Available for Input
    6. Check Available for Output
  3. Click Done

Step 4: Salesforce Flow – Add a Record Collection Variable to Store Opportunity Line items Records

  1. Under Manager, click New Resource to create a variable to store opportunity line item records.
  2. Input the following information: 
    1. Resource Type: Variable
    2. API Name: varR_OpportunityLineItems
    3. Data Type: Record
    4. Click the Yes checkbox – Allow multiple values (collection)
    5. Object: OpportunityLineItem
    6. Check Available for Input
    7. Check Available for Output
  3. Click Done

Step 5: Salesforce Flow – Using Decision Element to Check if Opportunity is Closed Won or Not

Now we will use the Decision element to check the StageName to ensure that it is equal to Closed. 

  1. On Flow Designer, click on the + icon and select 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.StageName}
      2. Operator: Equals
      3. Value: Closed Won

Step 6: Salesforce Flow – Add a Create Records Element to Generate Renewal Opportunity 

 The next step is to create a new Opportunity, for this we will use the Create Records element.

  1. On Flow Designer, click on the + icon and select Create Records element. 
  2. 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
    5. Set Field Values for the Opportunity
    6. Row 1:
      1. Field: CloseDate
      2. Value: {!forD_RenewalOpportunityCloseDate}
    7. Click Add Field
    8. Row 2:
      1. Field: Name
      2. Value: {!$Record.Name}
    9. Click Add Field
    10. Row 3:
      1. Field: Pricebook2Id
      2. Value: {!$Record.Pricebook2Id}
    11. Click Add Field
    12. Row 4:
      1. Field: RecordTypeId
      2. Value: {!$Record.RecordTypeId}
    13. Click Add Field
    14. Row 5:
      1. Field: StageName
      2. Value: Prospecting
    15. Click Add Field
    16. Row 5:
      1. Field: AccountId
      2. Value: {!$Record.AccountId}
  3. Click Done.

If you are using currency on the opportunity and price book, make sure to map it. 

Step 7: Salesforce Flow – Adding a Get Record Element to Find Line Items From Closed Won opportunity 

The next step is to get all the Opportunity Line Items from the old opportunity.

  1. On Flow Designer, click on the + icon and select Get Records element. 
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Select the Opportunity Product object from the dropdown list.
  4. Select All Conditions Are Met (AND)
  5. Set Filter Conditions
    1. Row 1:
      1. Field: OpportunityId
      2. Operator: Equals
      3. Value: {!$Record.Id}
  6. How Many Records to Store:
    1. select All records
  7. How to Store Record Data:
    1. Choose the option to Automatically store all fields
  8. Click Done.

Step 8: Salesforce Flow – Using Decision Element to Check if Opportunity Line Items record(s) Exist or Not

Now we will use the Decision element to check the Record Collection Variable from step 7 to find if it returns the Opportunity line items or not. 

  1. On Flow Designer, click on the + icon and select 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_Line_Items}
      2. Operator: Is Null 
      3. Value: {!$GlobalConstant.False}
  5. Click Done.

Step 9: Salesforce Flow – Add a Loop Element to Extract Records from the Record Collection Variable

The next step is to extract the opportunity line items from Record Collection Variable from step 7 one by one. For this, we will use the Loop Element.

  1. On Flow Designer, click on the + icon and select Loop element. 
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. For the Record Collection Variable select {!Get_Line_Items}.
  4. For Specify Direction for Iterating Over Collection select the option First item to last item
  5. Click Done.

Step 10: Salesforce Flow – Assignment Element to populate OpportunityId to a Record Variable 

The next step is to assign the data from the loop element to varR_OpportunityLineItem record variable. We will use an Assignment element. 

  1. On Flow Designer, click on the + icon and select Assignment element. 
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Set Variable Values
    1. Row 1:
      1. Field: {!varR_OpportunityLineItem.OpportunityId}
      2. Operator: Equals
      3. Value: {!Create_Renewal_Opportunity}
    2. Add Assignment
    3. Row 2:
      1. Field: {!varR_OpportunityLineItem.PricebookEntryId}
      2. Operator: Equals
      3. Value: {!Line_Items.PricebookEntryId}
    4. Add Assignment
    5. Row 3:
      1. Field: {!varR_OpportunityLineItem.UnitPrice}
      2. Operator: Equals
      3. Value: 0
    6. Add Assignment
    7. Row 4:
      1. Field: {!varR_OpportunityLineItem.Quantity}
      2. Operator: Equals
      3. Value: {!Line_Items.Quantity}
  4. Click Done.

Step 11: Salesforce Flow – Add an Assignment to Add Line Item to a Record Collection Variable (Created in step 4) 

The next step Is to add record variables from step 3 into a Record Collection Variable which we created in step 4, So at the end, we will create records in OpportunityLineItem

  1. On Flow Designer, click on the + icon and select Assignment element. 
  2. Enter a name in the Label field; the API Name will auto-populate.
  3. Set Variable Values
    1. Row 1:
      1. Field: {!varR_OpportunityLineItems}
      2. Operator: Add
      3. Value: {!varR_OpportunityLineItem}
  4. Click Done.

Step 12: Salesforce Flow – Add a Create Records Element to Insert Opportunity Line Items to Renewal Opportunity 

 The next step is to create line items for renewal opportunity, for this we will use the Create Records element.

  1. On Flow Designer, click on the + icon and select Create Records element. 
  2. Input the following information:
    1. Enter Label the API Name will auto-populate.
    2. How Many Records to Create: Multiple
    3. Select Values to Create Multiple Records: 
      1. Record Collection: {!varR_OpportunityLineItems}
  3. Click Done.

In the end, Steven’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-generate renewal Opportunity with OLI {!$Flow.CurrentDateTime}
  6. Click Save

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

👉 Check out the video for step-by-step instructions.

Proof of Concept

Now onwards if a business user updates the Opportunity Stage to Closed Won, then Salesforce Flow will automatically fire and create a renewal opportunity with line items. 

  1. Opportunity Aethna Home Products is in Stage Qualification and three products are associated with it, as shown in the following screenshot:
  2. Now update the Opportunity status to Closed-Won and check the renewal opportunity by navigating to the Opportunity tab.

Guided Practice (We-do):

At the moment, the renewal opportunity is not linked with an account. Update the flow to make sure that is auto associate with the same account as a closed-won opportunity. 

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)

87 thoughts on “Auto-generate Renewal Opportunity With Line Items

  1. Hi Rakesh,

    I managed to create the flow succesfully and even added Contact Roles, but can you make sure that Notes are being carried to the New Opportunity? Can’t seem to find the available field in my Object.

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