Sub topic 3
Variables, Collections, and Formulas
This chapter assumes you know the Salesforce data model (objects, fields, records) and can open Flow Builder from Setup. It covers the resources that hold data inside a flow, which later chapters use for input and output variables, loops, and bulk-safe design.
What resources are
A resource is a named value that you create once and reuse across a flow's elements. You refer to it by its API name, such as varAccountId. Flow Builder offers several kinds. This chapter covers four: variables, collections, formulas, and constants.
| Resource | Holds | Changes during the run? |
|---|---|---|
| Variable | One value or one record | Yes |
| Collection variable | A list of values or records | Yes |
| Formula | A value computed from other resources | Recalculated on each use |
| Constant | One fixed value | No |
Variables
A variable holds a single value. You choose its data type when you create it: Text, Number, Currency, Boolean, Date, Date/Time, Picklist, or Record. A record variable holds one record of a chosen object.
Two checkboxes matter later in this tutorial. "Available for input" lets a caller pass a value in when the flow starts. "Available for output" lets a caller read the value when the flow ends. Both are off by default. If you forget to turn one on, the caller cannot see the variable.
You change a variable's value with an Assignment element. Get Records, Create Records, and Loop elements can also write to variables.
Collections
A collection variable is a list. You create one by choosing the same data types as a variable and selecting "Allow multiple values". A record collection holds many records of one object.
Get Records returns a collection when you choose "All records". You then use a Loop to visit each record, and an Assignment to add changed records to a second collection. After the loop, one Update Records element saves the whole collection.
Updating once after the loop matters because each data element inside a loop counts against governor limits on every pass. The chapter on bulk-safe design returns to this.
Formulas
A formula resource is a read-only expression that uses the same syntax as a formula field. It can reference other resources and use functions such as TODAY(), IF(), and TEXT(). For example, a Date formula that finds records older than 30 days:
TODAY() - 30You can then use that formula as a filter value in Get Records. A formula is recalculated each time the flow reads it, so a value like NOW() can differ between two reads in the same run.
Constants
A constant holds a value that never changes, such as a fixed email address or a status name. Using a constant instead of typing the text in several places means you edit it once.
Common errors
- Assigning a single record to a collection with the equals operator. Use the Add operator instead.
- Reading a variable that a Get Records element did not fill. If no record matches, a record variable is empty, so check with a Decision first.
- Expecting a formula to store a result. It computes a value on demand and cannot be assigned to.
Exercise
A flow needs to set the Status of every Case returned by Get Records to "Closed". Which resources and elements do you need, and where should the Update Records element go?
Show answer
You need the record collection from Get Records, a Loop, an Assignment that sets Status on the current loop item, and a second record collection that receives each changed record with the Add operator. Place one Update Records element after the loop so it saves the second collection in a single operation.