Flow / Admins

Advanced Flow Logic: Data Elements, Collections, Loops, and Assignments

By Tim Combridge

Salesforce Flow empowers Administrators and Declarative Developers to do some pretty powerful things in Salesforce. If you’ve been putting in the effort to learn Salesforce Flow and are already getting the hang of it, you may be looking to push your knowledge further to really master the tool. 

This article will teach you some of the key foundational skills of Advanced Salesforce Flow Logic, enabling you to take the leap and become a flow expert.

What is a Data Element?

You’ve probably already come across Data Elements in your flow journey, but as it’s arguably the most powerful and valuable element in the Salesforce Flow arsenal, it’s certainly something you want to understand in its entirety so you can harness it to its full potential.

When a Data Element is fired it triggers what’s known as a DML Statement. DML stands for Data Manipulation Language, and a DML Statement is basically a command that is sent to the Salesforce Database telling it to do a specific thing to some records. Although you’re not writing the code yourself, the Data Element does fire off code behind the scenes, and as such you’re bound by the same rules as a developer would be when they write code. These rules are known as Governor Limits.

READ MORE: What Are Salesforce Governor Limits? Best Practices & Examples

The Different Types of Data Elements

There are four different types of Data Elements in Salesforce Flow: Get Records, Create Records, Update Records, and Delete Records.

Get Records

This Data Element allows you to look up records from the Salesforce database using SOQL (don’t worry, you don’t need to write any code yourself), based on criteria that you set. You can choose to get a single record or multiple records. Once this Data Element is executed by the system in a Flow Interview, the resulting records are saved into a variable (either one that you create and have the values assigned to or a dynamic variable that Salesforce creates) and can be used by the remainder of your flow.

Create Records

You can use this Data Element to insert a record (or multiple records) into the Salesforce Database. You can create new records either one-by-one or in bulk using a Collection Variable (more on Collections later). If you’re creating a single record, you can assign each field value individually within the Create Records Data Element rather than using a variable.

Update Records

Make changes to existing records in your Salesforce Database with the Update Records Data Element. Simply specify the criteria of the records you’d like to make changes to, and then specify the values you’d like to set. You can update one or more records using the Update Records element.

Delete Records

Finally, you can delete existing records from your Salesforce Database using the Delete Records Data Element. Simply set the criteria for the records you’d like removed, and they will be deleted when the Delete Records Data Element within the flow is executed.

When Should I Use a Data Element?

Whenever you need to interact with the Salesforce Database, you’ll need to use a Data Element. Basically, when you want to give your flow the context of your Salesforce environment – even if you just want to display some record information – you’ll need to use a Data Element. 

An example of this would be when you want to create multiple Task records in your Salesforce database. A Create Records Data Element could be used to do this, with the input variable equal to a Collection Variable (more on Collection Variables in the next section).

What is a Collection?

A Collection (also known as a Collection Variable) is used when you have more than one variable that you want to group together. These may be Record Variables or other variable types (Text, Number, etc). 

Collections behave very similarly to Apex Lists in that they group multiple variables together and are ordered (whereas Apex Sets are not ordered).

When Should I Use a Collection?

You should be using a Collection anytime you want to perform the same action on a group of variables or collect multiple variables together for easier organization within your flow. One prime example of when a Collection is almost essential is when you want multiple variables involved with a Data Element. 

For example, if you want to insert 20 different Task records like the example above, you should group them together into a Collection Variable and then use that Collection as the input for your Create Records element. This means that instead of firing the Create Records Data element 20 separate times, it will fire just once but insert all 20 records.

What is a Loop?

A Loop lays the foundation for circular logic within a flow in Salesforce. Putting a Loop on the Flow Builder Canvas allows you to set a series of actions that need to be repeated until each variable in a Collection has been actioned.

For example, a Loop could be used to create a new Task record for each Opportunity in a Collection that is a certain amount of days out from its Due Date. The Loop would perform a series of actions against each of the Opportunity record variables in a Collection.

When Should I Use a Loop?

Loops are great but should be used cautiously. Firstly, you should NEVER use a Data Element within the context of a Loop. As you’ve already read in this article, these Data Elements perform DML Statements which are governed by Salesforce Governor Limits. By putting a Data Element within a Loop you’ll be building a flow that doesn’t have a natural limit on the number of DML Statements that are executed within a single transaction

One common use of a Loop is when you need to add multiple records to a Collection to be inserted into Salesforce, based on another set of records. For example, if you want to create a Task record for each of the Opportunity records in a Collection. The input Collection Variable would be the Opportunity Collection and the Loop would contain two Assignment Elements (more on Assignment further down in this article). The Loop allows for the actions to be repeated as many times as there are variables in the Collection.

What Is an Assignment?

Think of an Assignment as a “Create/Update Element”, except instead of creating a record in the Salesforce Database it’s creating a draft version of it that is ready to be inserted later. The variable type is set and values are assigned, but no DML Statement is executed.

Assignments can also be used to add variables to Collection variables. This is a great way of constructing the Collection one variable at a time where necessary.

When Should I Use an Assignment?

Continuing the example from the Loops section, let’s dive deeper into the Assignment elements that sit within the Loop. This format is quite a common use case for both Loops and Assignment elements. 

The first Assignment in this example is used to assign field values to a record variable. The Task Subject, Description, Due Date, and WhatId (the current Opportunity record from the Loop) in the new Task variable are being set by the Assignment element.

The second Assignment is simple but extremely important and effective. It is assigning the Task variable that was created by the first Assignment into a new Collection of Task record variables. This is so that it can be inserted into the database using a Create Records element (yep, just like the example in the ‘Data Elements’ section above!).

Summary

Salesforce Flow empowers Administrators and Declarative Developers (point-and-click developers who don’t use code) to do some pretty powerful things in Salesforce. Using clicks, not code, they are able to embed complex business processes and logic into the backend of Salesforce and have them fired automatically. They can also create user-friendly screen interfaces with useful functionality, which ultimately saves the users time and the business money by cutting down on the risk of human error. Salesforce Flow: Power to the People!

If you’re looking for an even deeper understanding of Advanced Flow Logic, you may want to consider looking at the Ultimate Salesforce Flow Foundation Course by Salesforce Ben.

The Author

Tim Combridge

Tim is the Managing Director at Sensible Giraffe, passionately educating others via high-quality blog content and training courses including the Ultimate Salesforce Flow Foundation Course.

Leave a Reply