Lightning Flow Builder: A how-to guide with examples
By: Keerthana Pachalla
Tags: Salesforce, CRM, Lightning Flow, Women In Tech, Salesforce Automation

With Spring ’19, Salesforce replaced the Cloud Flow Designer with the Lightning Flow Builder. If you’ve used the Cloud Flow Designer extensively, this might not be such a big switch but just takes some getting used to. If you’ve never used flows and feel like it is a huge learning curve, fret not! Lightning Flow Builder is in fact more intuitive and streamlined. With the advantage of point and click, Salesforce admins can now achieve complex flows with little to no Apex code!

The advantages of flows are:

If your organization does not have a large team of developers, flows provide an alternative and has a much more gradual learning curve as compared to Apex code. If you’re delivering for a client team, flows are easier to maintain by providing a intuitive interface for admins to make changes in the long term. Time and effort to create Apex code test classes is eliminated (except when invoking the Flow using Apex — Use Case #2) While with Apex you have to think about bulkification to avoid governor limits, Flow interviews automatically bulkify actions (*Note on this at the end of the article) Flow Builder also provides a visual debugging interface which is intuitive and makes troubleshooting smoother.

Always remember the Salesforce mantra — Keep it Simple and pick the best solution for your use case.

Before we jump into examples, here’s a quick cheat sheet of the modules available in the Flow Builder:

Roll up your sleeves and let’s get building! Here are two relatively common and simple use cases to introduce and build out flows using Flow Builder. To provide a couple of alternatives, the examples showcase invoking a flow from both a Process Builder (Use Case #1) and an Apex trigger (Use Case #2).

Use Case #1: Create child records for a set of parent records

Consider a very large sales team spread out across multiple departments in an organization, where specific sales rep should only get access to opportunities at a certain stage, based on the opportunity type due to confidentiality clauses. In this Sales team example, our solution will query the database to find a list of users that are part of Market Research department based on the opportunity stage, and add all these users to an opportunity team. We ultimately want to assign people dynamically and not through public groups because the sales reps regularly cycle through departments (and there are limitations when it comes to adding a group to an opportunity team).

Step 1: Start with dragging and dropping the “Get Records” function to create a list of all the sales reps that need to be on the opportunity

In our Get Records function, we’ll set up some conditions for the users that we want to round up. In this example, lets start with active users where the Department is Market Research.

Next we are going to create a resource (think variable) to save all of these users into a list. You can either do this from the resources tab on the left or “+” sign under “Record Collection Variable”. We will select the data type as Record and since this is a list, we will allow multiple values.

Select the variable we created, and we are going to save two attributes, ID and Name. Additional attributes can be added if you want to use those values in your next function.

Step 2: Next we will use the “Loop” function to loop through our list and perform additional functions on them

In our Loop function, we’ll select the record collection variable we are going to loop through (the one created in the step above).

For each loop we need to associate a “loop variable”. At any point in the loop, this variable will point to the specific record so that we can extract/manipulate the values. Think of it as a single record as compared to the list of records.

Loop Variable Resource

Step 3: Next we will use the “Create Records” function to create a list of all the sales reps that need to be on the opportunity.

For this example, since we are inserting into the opportunity team member object and don’t have all the values in our record collection variable, we’ll then use separate variables option. This gives us the flexibility to type in some picklist values. We select the opportunity team member object to create records.

Next we will assign values. We’ll use the loop variable to assign the ID and name that we saved. Flow easily shows you the available fields and picklist values within the opportunity team member making the assignment a very simple process. We also created two simple text variables — one to denote the current opportunity (make sure the “input variable” option is checked), i.e. the opportunity we invoke the flow from and a text variable to store the opportunity team member record ID post create. More on these later.

Now that the records are created, let’s add an email alert action so all the market reps are notified that they are added to the opportunity. Here, we’ll select an existing email alert, and for the record ID, we’ll use the OppTeamMemberID variable we stored while creating the records.

Step 4: Now for the fun part! We connect everything we just built in the order of operations!

Since we are setting up one record and email alert at a time, make sure you close the loop through those actions

Step 5: Now your flow is ready to be invoked! For the first example we’ll invoke this flow using a simple process builder.

The key step is to select Flow in the actions. Select the flow we created. The flow variables list will then show all the variables that are set to “input variable”. We’ll set this field to the current opportunity ID. This is how we pass current record info from Process builder to flow!

Step 6: That’s it! Activate your process builder and flow and get testing!

A great way to start testing is to use the “debug” feature on the flow.

Tip: In the debug section, just select any opportunity ID that exists in your current environment as a test. Note: The debug feature WILL create new records on your opp, so make sure you don’t debug it directly in production :)