trailhead

Combine the Power of Process Builder and Cloud Flow Designer

Learning Objectives

After completing this unit, you'll be able to:

  • Describe a business process that can be automated using a process and a flow.
  • Define what a flow variable is.
  • Build a flow that iterates over a group of records.
  • Build a process that starts a flow.

When Process Builder Isn’t Enough

Process Builder isn’t designed to address every possible use case, so you may find that it can automate parts of your business process, but not all. For example, Process Builder can’t:

  • Post to a community feed
  • Submit a related record for approval
  • Delete records
  • Create a bunch of records and associate them with each other
  • Perform complex logic

Goods news: You don’t have to rebuild the whole thing in another tool. Configure the more complex functionality in a flow, and then add a flow action to your process. If a flow also can’t do what you need, you or a developer can write Apex to do so. Then add an Apex action to your process.

Clone Records with Children

You've been asked to automatically create renewal opportunities when an opportunity is closed won. The renewal should be a clone of the original opportunity. We can clone records in Process Builder, but we also need to clone the products and associate them with the renewal opportunity.

In Process Builder, you can’t grab the ID of the created record and use it elsewhere. Luckily, you can do so in a flow. We build a flow that clones the opportunity and its products, and we build a process that calls the flow when an opportunity is closed won.

Beyond the Basics

In the last unit, we talked about flows that guide users through a business process with screens. We call those screen flows. You can also build autolaunched flows, which run in the background like a process. The main difference is that autolaunched flows can’t have screens, which require user interaction. Because they have no screens, you can call autolaunched flows from backend things like processes and Apex classes.

Build a Process

Instead of creating another process, let’s build on the one we created in an earlier unit.
The Closed Won Opportunities process from an earlier unit

In the Closed Won Opportunities process, add a criteria node named Closed Won with this filter condition.

Field Value
Opportunity > Stage Closed Won

We can't configure the actions in Process Builder, so let’s save and switch to Cloud Flow Designer.

Build a Flow

From Setup, enter Flows in the Quick Find box, then select Flows, and click New Flow.

We’re building a flow that:

  • Clones an opportunity
  • Clones opportunity products
  • Associates the cloned products with the cloned opportunity

To do so, the flow needs some data from the process: the original opportunity and its opportunity products.

Flow variables come in four types.

Type Can store... Example
Variable A single value “Hello World”, true, 6
sObject Variable A set of field values for a single record Rating, ID, and Name for an account
Collection Variable Multiple values of the same data type [1, 2, 3, 5, 8, 13]
sObject Collection Variable A set of field values for multiple records that have the same object Rating, ID, and Name for multiple accounts

To store the opportunity and its opportunity products, we create an sObject variable and an sObject collection variable. When we add the flow as an action in the process later, we pass data into these variables, so they must allow input access.

  1. From the Resources tab, double-click sObject Variable and set these values.
    Unique Name opportunity
    Input/Output Type Input Only
    Object Type Opportunity
  2. Double-click sObject Collection Variable and set these values.
    Unique Name oppProductsOriginal
    Input/Output Type Input Only
    Object Type OpportunityLineItem

Clone the Opportunity

When the flow is called by the process, the {!opportunity} variable will contain fields from the original opportunity. Before we clone that opportunity, let’s update some of the fields. That way, the renewal opportunity doesn’t start off as Closed Won.
  1. From the Palette tab, drag an Assignment element onto the canvas.
  2. Name it Update Opportunity Fields.
  3. Set the stage to Prospecting.
    1. For Variable, select SOBJECT VARIABLES | opportunity | StageName.
    2. For Operator, select equals.
    3. For Value, select PICKLIST VALUES | Prospecting.
  4. Set CloseDate to 90 days from today.
    1. Click Add Assignment.
    2. For Variable, type Close and select SOBJECT VARIABLES | opportunity | CloseDate.
    3. For Operator, select equals.
    4. For Value, select CREATE NEW | Formula and set these values.
      Unique Name ninetyDays
      Value Data Type Date
      textbox TODAY() + 90
  5. Click OK twice.
  6. To create the opportunity, drag a Fast Create element onto the canvas and set these values.
    Name Clone Opportunity
    Variable SOBJECT VARIABLES > opportunity
  7. Click the node at the bottom of Update Opportunity Fields and drag it to Clone Opportunity.

When the opportunity is created, the ID field in that variable ({!opportunity.Id}) is populated with the new record’s ID. We reference that value later to associate the new products with the renewal opportunity.

Clone the Opportunity Products

Now comes the fun part: cloning the opportunity products.

When the flow is called by the process, the {!oppProducts_Original} variable contains fields from the original opportunity products. Before we clone those products, we need to associate them with the renewal opportunity instead of the original one and set the total price for each product to null. (Opportunity products can’t have both a unit price and a total price.)

The only way to update items in a collection is to iterate over the collection with a loop.

A loop can reference only one item at a time. Because sObject collection variables store multiple items, you need a way to reference a given item. A loop variable is a variable that the loop uses while iterating over the collection. When a loop starts, the first item in the collection variable is copied into the loop variable. Any elements inside the loop then act on the loop variable. Once the iteration is over, the loop variable is overwritten with the next item’s values. And so on, until there are no items left in the collection.

A collection item is copied into a loop variable. The loop variable data is updated and then copied to create a new item in another collection. The loop variable is then updated by a copy of the next item in the original collection.
Tip

Tip

  • To update an item’s field values inside a loop, update the loop variable. Then, before the iteration for that item finishes, add the loop variable as an item in another collection variable. Otherwise, the changes are overwritten when the next item is loaded into the loop variable.
  • Avoid adding actions, like creating or updating records, inside a loop. That’s a surefire way to hit limits.

In the loop, we update each item’s Opportunity ID and Total Price, and then we add the item to a new collection variable. After the loop, we use the new collection variable to create the opportunity products.

  1. Drag a Loop element onto the canvas, and set these values.
    1. Name it Iterate Over Products.
    2. For Loop through, select SOBJECT COLLECTION VARIABLES | oppProducts_original.
      This option tells the loop which collection to iterate over when assigning items to the loop variable.
    3. For Loop Variable, create an sObject variable with these values, then click OK twice.
      Unique Name oppProduct_loop
      Object Type OpportunityLineItem
  2. Connect Clone Opportunity to the loop.
  3. Set the loop variable’s opportunity ID to the ID of the new opportunity and its total price to null.
    1. Drag an Assignment element onto the canvas.
    2. Name it Update Opportunity Product.
    3. Click Add Assignment, and configure these assignments.
      Variable Operator Value
      SOBJECT VARIABLES > oppProduct_loop > OpportunityId equals SOBJECT VARIABLES > opportunity > Id
      SOBJECT VARIABLES > oppProduct_loop > TotalPrice equals Leave blank
      Flow assignments that set the loop variable's Opportunity ID and Total Price fields.
    4. Click OK.
  4. Connect the loop to Update Opportunity Product. Verify that Go to this element is set to for each value in the collection.
  5. Add the updated loop variable to a new sObject collection variable.
    1. Drag another Assignment element onto the canvas.
    2. Name it Add to New Collection.
    3. For Variable, create an sObject collection variable with these values, then click OK.
      Unique Name oppProducts_new
      Object Type OpportunityLineItem
    4. For Operator, select add.
    5. For Value, select SOBJECT VARIABLES | oppProduct_loop.
    6. Click OK.
  6. Connect Update Opportunity Product to Add to New Collection. Then connect Add to New Collection to the loop.
  7. To clone the opportunity products, drag a Fast Create element onto the canvas and set these values.
    Name Clone Products
    Variable SOBJECT COLLECTION VARIABLES > oppProducts_new
  8. Connect the loop to Clone Products.
The finished flow

Finish the Flow

You’re nearly done! To finish the flow, designate the starting element and activate the flow.
  1. Hover over the top right corner of Update Opportunity Fields, and click Set a Start element.
  2. Save the flow, and name it Renew Opportunity.

Close the Cloud Flow Designer, and activate the flow so that you can reference it in Process Builder.

Finish the Process

Now that we’ve finished building the flow, let’s make sure that the process starts the flow when an opportunity is Closed Won. Remember those variables we created at the beginning of the flow? In the process, we use them to pass values from the opportunity record and its children into the flow.
  1. Open the Closed Won Opportunities process.
  2. In the Closed Won criteria group, add an immediate action where the type is Flows and the name is Create Renewal.
  3. For Flow, select Renew Opportunity.
  4. Under Set Flow Variables, add two rows, set these values, then click Save.
    Variable Type Value
    opportunity Field Reference Select the Opportunity record that started the process
    oppProducts_original Field Reference Opportunity > OpportunityLineItems
  5. Make sure that both of the criteria nodes are always evaluated.
    When an opportunity is closed won, the renewal should always be created. But we need to make sure that draft contracts and follow-up tasks are created if the opportunity is also high value.
    1. Drag the Closed Won criteria node above Closed Won and High Value.
    2. For the Closed Won criteria group, click STOP. Select Evaluate the next criteria, then save.
The final process

To start using the process, activate it.

Resources

Lightning bolt icon used to indicate that the content is for Lightning Experience

Remember, this module is meant for Lightning Experience. When you launch your hands-on org, switch to Lightning Experience to complete this challenge.

retargeting