Combine the Power of Process Builder and Flow Builder
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.
But there’s good news! You don’t have to rebuild the whole automation in another tool. Configure the more complex functionality in a flow, and then add a flow action to your process. If a flow 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 that in a flow. Just build a flow that clones the opportunity and its products, and 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

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 Flow Builder.
Build a Flow
We’re building a flow that:
- Clones an opportunity.
- Clones opportunity products.
- Associates the cloned products with the cloned opportunity.
To do all that, 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 |
Collection Variable | Multiple values of the same data type | [1, 2, 3, 5, 8, 13] |
Record Variable | A set of field values for a single record | Rating, ID, and Name for an account |
Record Collection Variable | A set of field values for multiple records of the same object type | Rating, ID, and Name for multiple accounts |
To store the opportunity and its opportunity products, we create a record variable and a record collection variable. Later, when we add the flow as an action in the process, we pass data into these variables, so they must allow input.
- From the Manager in Toolbox, click New Resourceand set these values.
Field Value Resource Type Variable API Name opportunity Data Type Record Object Opportunity Available for input Selected - Click Done.
- Click New Resourceand set these values.
Field Value Resource Type Variable API Name oppProducts_Original Data Type Record Allow multiple values (collection) Selected Object Opportunity Product Available for input Selected - Click Done.
Clone the Opportunity
- From the Elements in the Toolbox, drag an Assignment element onto the canvas.
- In Label, name it Update Opportunity Fields.
- Set the stage to Prospecting.
- For Variable, select RECORD (SINGLE) VARIABLES | opportunity | StageName.
- For Operator, select Equals.
- For Value, select PICKLIST VALUES | Prospecting.
- Set Close Date to 90 days from today.
- Click Add Assignment.
- For Variable, select RECORD (SINGLE) VARIABLES | opportunity | CloseDate.
- For Operator, select Equals.
- For Value, select New Resourceand set these values.
Field Value Resource Type Formula API Name ninetyDays Data Type Date
- In Formula, leave Insert a resource… blank and enter TODAY() + 90 in the text box.
- Click Done and Done.
- To create the opportunity, drag a Create Recordselement onto the canvas and set these values.
Field Value Label Clone Opportunity Record RECORD (SINGLE) VARIABLES > opportunity
- Click Done.
- Click the node at the bottom of Start and drag it to Update Opportunity Fields.
- 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
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 opportunity 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 tells the flow to process each item in the collection one at a time, executing the same logic on each item until the entire collection has been processed.
Each opportunity product in our {!oppProducts_Original} record collection should be associated with the renewal opportunity, and the total price set to null.
Each time the loop iterates, the loop variable represents an item in the collection. When a loop starts, the first item in the collection variable is copied into the loop variable. After 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.

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.
- Drag a Loop element onto the canvas, and set these values.
- In Label, name it Iterate Over Products.
- For Collection Variable, select RECORD COLLECTION VARIABLES | oppProducts_Original. This option tells the loop which collection to iterate over when assigning items to the loop variable.
- Click Done.
- Connect Clone Opportunity to the loop.
- Set the loop variable’s opportunity ID to the ID of the new opportunity and its total price to null. Drag an Assignment element onto the canvas.
- In Label, name it Update Opportunity Product.
- In Set Variable Values, configure these assignments. Click Add Assignmentto add a second line.
Variable Operator Value RECORD (SINGLE) VARIABLES > Current Item from Loop Iterate_Over_Products > OpportunityId Equals RECORD (SINGLE) VARIABLES > opportunity > Id RECORD (SINGLE) VARIABLES > Current Item from Loop Iterate_Over_Products > TotalPrice Equals Leave blank
- Click Done.
- Connect the loop to Update Opportunity Product. The Select loop connector window appears.
- Verify that For each item in the collection is selected in the Loop Connector field and click Done.
- Add the updated loop variable to a new record collection variable.
- Drag another Assignment element onto the canvas.
- In Label, name it Add to New Collection.
- For Variable, select New Resourceand create a new resource variable with these values.
Field Value Resource Type Variable API Name oppProducts_new Data Type Record Object Opportunity Product Allow multiple values (collection) Selected - Click Done.
- For Operator, select Add.
- For Value, select RECORD (SINGLE) VARIABLES | Current Item from Loop Iterate_Over_Products. Delete the period between {!Iterate_Over_Products and the right bracket } and then click outside of the Value field.
- Click Done.
- Connect Update Opportunity Product to Add to New Collection. Then connect Add to New Collection to the loop.
- To clone the opportunity products, drag a Create Recordselement onto the canvas and set these values.
Field Value Label Clone Products How Many Records to Create Select Multiple Record Collection Variable RECORD COLLECTION VARIABLES > oppProducts_new - Click Done.
- Connect the loop to Clone Products.

Finish the Flow
- Click Save.
- Save the flow, and name it Renew Opportunity. Click Show Advanced and make sure that Type is set to Autolaunched Flow.
- Click Save.
Activate the flow so that you can reference it in Process Builder. Close Flow Builder.
Finish the Process
- Open the Closed Won Opportunities process.
- In the Closed Won criteria group, add an immediate action where the type is Flows and the name is Create Renewal.
- For Flow, select Renew Opportunity.
- Under Set Flow Variables, add two rows, set these values, and then click Save.
Variable Type Value opportunity Field Reference Select the Opportunity record that started your process oppProducts_original Field Reference Opportunity > OpportunityLineItems - Drag the Closed Won criteria node above Closed Won and High Value.
- For the Closed Won criteria group, click STOP.
- Select Evaluate the next criteria and then click Save.
- To start using the process, activate it.
Resources
- Trailhead Project: Build a Discount Calculator