Getting Started with Process Builder – Part 30 (Auto-generate Renewal Opportunity With Line Items)

Getting Started with Process Builder – Part 30 (Auto-generate Renewal Opportunity With Line Items)

Last Updated on December 2, 2020 by Rakesh Gupta

Big Idea or Enduring Question:

How do you automatically create a new opportunity by cloning an existing opportunity with 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 Process Builder to auto-generate renewal Opportunity
  • How to use get the element to find the old record details
  • 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

Automation Champion Approach (I-do):

There are a few possible solutions for the above business scenario, but I’ll use Process Builder and 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 2 steps to solve Steven’s business requirement using Flow and Process Builder. We must:

  1. Lightning Flow Steps:
    1. Define flow properties for auto-launched flow
    2. Add a Record variable to store old opportunity record data
    3. Create a Formula field to generate a close date for a renewal opportunity 
    4. Add a Record create variable to store opportunity line item
    5. Add a Record collection create variables to store opportunity line items
    6. Add a Create Records element to generate renewal opportunity
    7. Add a Get Records element to find line items for the old (closed won) opportunity 
    8. Add a Recision element to check the id from the record collection variable (from step 1.7)
    9. Add a Loop element to extract records from the collection variable (from step 1.7)
    10. Add an Assignment to populate line item(s) and new Opportunity id to a record variable
    11. Add an Assignment to add line item (record variable from 1.7) to a record collection variable (for bulk update)
    12. Add a Create Records element to insert line Items to renewal opportunity
  2. Process Builder Steps:
    1. Define process properties
    2. Define evaluation criteria
    3. Define process criteria
    4. Add action – flows 

Step 1.1: Lightning 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 Autolaunched Flow (No Trigger) option and click on Next and configure the flow as follows: 
    1. How do you want to start building: Freeform
  5. Click Done.

Step 1.2: Lightning Flow – Add a Record Variable to Store Old Opportunity Record

  1. Under Toolbox, select Manager, then click New Resource to store old opportunity record.
  2. Input the following information: 
    1. Resource Type: Variable
    2. API Name: varROpportunity
    3. Data Type: Record
    4. Object: Opportunity
    5. Check Available for Input
    6. Check Available for Output
  3. Click Done.

Step 1.3: Lightning Flow – Create a Formula to Generate a Close Date For a Renewal Opportunity 

  1. Under Toolbox, select Manager, then click New Resource to create a formula for renewal opportunity close date.
  2. Input the following information: 
    1. Resource Type: Variable
    2. API Name: forDRenewalOpportunityCloseDate
    3. Data Type: Date
    4. Formula: {!varROpportunity.CloseDate} + 365
  3. Click Done.

Step 1.4: Lightning Flow – Add a Record Variable to Store Opportunity Line Item Record

  1. Under Toolbox, select Manager, then click New Resource to store opportunity line item record.
  2. Input the following information: 
    1. Resource Type: Variable
    2. API Name: varROpportunityLineItem
    3. Data Type: Record
    4. Object: OpportunityLineItem
    5. Check Available for Input
    6. Check Available for Output
  3. Click Done

Step 1.5: Lightning Flow – Add a Record Collection Variable to Store Opportunity Line items Records

  1. Under Toolbox, select Manager, then click New Resource to store opportunity line item records.
  2. Input the following information: 
    1. Resource Type: Variable
    2. API Name: varROpportunityLineItems
    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 1.6: Lightning 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. Under Toolbox, select Elements. Drag and drop Create Records onto the canvas. 
  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: {!forDRenewalOpportunityCloseDate}
    7. Click Add Row
    8. Row 2:
      1. Field: Name
      2. Value: {!varROpportunity.Name}+
    9. Click Add Row
    10. Row 3:
      1. Field: Pricebook2Id
      2. Value: {!varROpportunity.Pricebook2Id}
    11. Click Add Row
    12. Row 4:
      1. Field: RecordTypeId
      2. Value: {!varROpportunity.RecordTypeId}
    13. Click Add Row
    14. Row 5:
      1. Field: StageName
      2. Value: Prospecting
  3. Click Done.

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

Step 1.7: Lightning 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 existing opportunity.

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

Step 1.8: Lightning Flow – Using Decision Element to Check the Record Collection Variable (from step 1.7)

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

  1. Under Toolbox, select Element
  2. Drag-and-drop Decision element onto the Flow designer. 
  3. Enter a name in the Label field; the API Name will auto-populate.
  4. Under Outcome Details, enter the Label the API Name will auto-populate.
  5. Condition Requirements to Execute OutcomeAll Conditions Are Met (AND)
    1. Row 1:
      1. Resource: {!Find_Line_Items_from_Closed_Won_opportunity}
      2. Operator: Is Null 
      3. Value: {!$GlobalConstant.False}
  6. Click Done.

Step 1.9: Lightning Flow – Add a Loop Element to Extract Records from the Record Collection Variable (from step 1.7)

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

  1. Under Toolbox, select Element
  2. Drag-and-drop Loop element onto the Flow designer. 
  3. Enter a name in the Label field; the API Name will auto-populate.
  4. For the Collection Variable select {!Find_Line_Items_from_Closed_Won_opportunity}
  5. For Specify Direction for Iterating Over Collection select the option First item to last item
  6. Click Done.

Step 1.10: Lightning Flow – Assignment Element to populate line item(s) and new Opportunity id to a Record Variable (from step 1.4)

The next step is to assign the data from the loop element and renewal opportunity id In the record variable which we created in step 1.4, we will use an Assignment element. 

  1. Under Toolbox, select Element
  2. Drag-and-drop Assignment element onto the Flow designer. 
  3. Enter a name in the Label field; the API Name will auto-populate.
  4. Set Variable Values
    1. Row 1:
      1. Field: {!varROpportunityLineItem.OpportunityId}
      2. Operator: Equals
      3. Value: {!Renewal_Opportunity}
    2. Click Add Row
    3. Row 2:
      1. Field: {!varROpportunityLineItem.PricebookEntryId}
      2. Operator: Equals
      3. Value: {!Extract_Record_1_by_1.PricebookEntryId}
    4. Click Add Row
    5. Row 2:
      1. Field: {!varROpportunityLineItem.UnitPrice}
      2. Operator: Equals
      3. Value: 0
    6. Click Add Row
    7. Row 2:
      1. Field: {!varROpportunityLineItem.Quantity}
      2. Operator: Equals
      3. Value: {!Extract_Record_1_by_1.Quantity}
  5. Click Done.

Step 1.11: Lightning Flow – Add an Assignment to Add Line Item (From Step 1.10) to a Record Collection Variable (From Step 1.4)

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

  1. Under Toolbox, select Element
  2. Drag-and-drop Assignment element onto the Flow designer. 
  3. Enter a name in the Label field; the API Name will auto-populate.
  4. Set Variable Values
    1. Row 1:
      1. Field: {!varROpportunityLineItems}
      2. Operator: Add
      3. Value: {!varROpportunityLineItem}
  5. Click Done.

Step 1.12: Lightning Flow – Add a Create Records Element to Insert 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. Under Toolbox, select Elements. Drag and drop Create Records onto the canvas. 
  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: {!varROpportunityLineItems}
  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: 50
  5. Interview Label: Auto-generate renewal Opportunity with OLI {!$Flow.CurrentDateTime}
  6. Click Save

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

Our next task is to create a Process on the Opportunity object to launch a Flow, only when an Opportunity is closed won.

Step 2.1: Define Process Properties

  1. Click Setup.
  2. In the Quick Find box, type Process Builder.
  3. Select Process Builder, then click New.
  4. Name the Process and click the Tab button. The API Name will populate. 
  5. As a best practice, always input a description
  6. The process starts when A record changes.
  7. Click Save.

Step 2.2: Define Evaluation Criteria

  1. Click on the Add Object node to begin selecting the evaluation criteria.
  2. Select the Opportunity object from the dropdown list.
  3. Start the process when a record is created or edited.
  4. Click Save.

Record Evaluation Criteria

Step 2.3: Define Process Criteria

  1. Click the Add Criteria node to begin defining the process criteria.
  2. Name the criteria.
  3. The criteria should execute actions when the conditions are met.
  4. Set Conditions
    1. Row 1
      1. Field: Opportunity | StageName
      2. Operator:  Equals
      3. Type: Picklist
      4. Value: Closed Won
  5. Select All of the conditions are met (AND)
  6. Click Advanced
  7. Select Yes to execute the actions only when specified changes are made to the record.
  8. Click Save.

The reason why we would select the Yes checkbox for the question — Do you want to execute the actions only when specified changes are made to the record? — is to allow the Process Builder to execute the actions only if the record meets the criteria now, but the values that the record had immediately before it was saved didn’t meet the criteria. This means that these actions won’t be executed when irrelevant changes are made.

Step 2.4: Add Action – Flows

  1. Below Immediate Actions, click Add Action.
  2. For Action Type, select Flows
  3. Name the action.
  4. Select the flow we just createdAuto-generate renewal Opportunity WIth OLI.
  5. Set Flow Variables: 
    1. Row 1:
      1. Flow Variable: varROpportunity
      2. Type: Field Reference
      3. Value: Select the Opportunity record that started your process
  6. Click Save.

In the end, Steven’s Process will look like the following screenshot:

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

Proof of Concept

Now onwards if a business user updates the Opportunity Stage to Closed Won, then Process Builder 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:

Identify a custom object in your org that should be shared based on the settings in a related object and create an autolaunched flow that will share the records without actual manual sharing

Post a picture of the sharing on Twitter @automationchamp, #AutomatedSharingwithFlow

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

83 thoughts on “Getting Started with Process Builder – Part 30 (Auto-generate Renewal Opportunity With Line Items)

Leave a Reply

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