Background:
As a longtime Salesforce Developer, Architect, and Consultant I have seen the evolution of low code tools go from mediocre to great! Historically I’ve found automation tools like process builder and great for simple automations. In the past few years I’ve found flows to be reliable, sophisticated, and capable of handling most customizations and automations on the Salesforce Platform.
In this post I want to walk through a simple exercise that can teach you some of the basics of flows.
Flow: The flow I want to walk you through will automatically create Asset records for each Opportunity Product and its quantity automatically when an Opportunity is closed
Value: Faster time to build, valuable automation for most Sales Cloud implementations, and a great exercise to learn the basics of querying records from flows, updating a collection of records, looping, and more.
Let’s get started...
Step 1: Configure your data model
The Asset object is native to Salesforce, but there’s a few configuration changes we should make to have it visible on Opportunities
A) Create a lookup field on the Asset object to reference the Opportunity object
B) Add the Asset related list to the Opportunity page layout
Step 2: Create the Flow
A) Create the Flow:
- Go to the Setup menu within Salesforce and use the quick find search bar on the right to find Flows.
- Click “New Flow” in the top right to create the flow we will be building
B) Set Entry Criteria:
- Choose “Recorded Trigger Flow” as the type of flow.
- For the object, choose Opportunity.
- For the trigger, choose A Record is Updated. For the entry criteria, choose All Conditions Are Met, then set the Stage to equal Closed Won. Note: if you want this flow to only execute on a specific Opportunity Record Type, use Formula Evaluates to True for the entry criteria. You will need to write a formula that checks if the the opportunity is the correct record type and that the opportunity stage is Closed Won.
- Lastly where is asks when to run the flow, choose Only when a record is updated to meet the condition requirements. This ensures you don’t created duplicate assets.
Step 3: Build Flow Logic
Now that you’ve created the flow and the entry criteria you are ready to start building the actual flow.
A) Query the related Opportunity Products
- The first thing we want to do is get all the the relevant Opportunity Products so we can loop over them later when we create a collection of Asset records to add. Under Run Immediately choose Get Records, add a label of your liking, and choose Opportunity Product as the object. Here’s a screenshot of how it should look.
B) Loop through all the related Opportunity Products
- Just after the Get Records element we just added, click the dot below it, and add the Loop element.
- Use the values in the screenshot below to set the criteria of the loop
Now you have a loop that will iterate over all the Opportunity Products related to the Opportunity that triggered the flow.
C) Add logic to our Loop
Now that we have a loop we need to create a collection of new Asset records for not just each Opportunity Product, but also for the quantity of each Opportunity Product. This is the trickiest part of the Flow, but have no fear it’s simpler than you would expect. The goal is to get the loop to look like this:
- At the top center of the Flow Designer, use the dropdown to change the layout from Auto-Layout to Free Form. This let’s you have more control on how the Loop should behave.
- On the left sidebar, drag the Assignment operator on the screen. Add a label of your liking. For the variable box, create a new variable that’s has a type of number and set it to the current loop iterations quantity (quantity of the current Opportunity Product). See the screenshot below
Drag an arrow from the loop to the new assignment as shown below.
3. Drag the Decision operator from the left sidebar on the Flow and set the criteria of the Decision as shown below
4. Drag the Default Outcome arrow back to the the Loop element. This Decision operator will allow your loop to loop over multiple times if the quantity of an opportunity product is greater than 1.
Now we need to create a collection of Assets records to insert.
- Drag the Assignment operator onto the Flow.
- Set the criteria for this assignment to use an new record variable (asset) and assign the values and shown below.
3. For the option the other option on the Decision element, drab the arrow to the new assignment we just created. We should now have a flow that looks something similar to the screenshot below.
Now we need to assign the Asset we just we just stored in a variable to a collection of Assets
- Drag another Assignment operator from the left sidebar to the flow.
- This assignment will add the Asset we just assigned to a collection of Asset variables.
- See the screenshot below for details
Lastly we need to reduce the counter we made that tracks how many assets we created depending on the quantity of each Opportunity Product.
- Drag the assignment operator from the left sidebar onto the flow.
- Set the criteria for the assignment to reduce the counter variable by 1. See the screenshot below.
Now we just need to connect the elements of the loop and our loop should be ready. Take a look at the screenshot below to make the proper connections
Now we should have a collection of new Assets that we can now insert into Salesforce.
- Drag the Create Records operator from the left sidebar onto the flow and set the criteria of this element as show below.
Lastly, connect the end of the loop to the create records operator we just created. See the screenshot below.
Now you have a flow ready to test and then activate!