Sub topic 3
Variables, Collections, and Formulas
This chapter covers the resources a flow uses to hold and compute data. It assumes you know the Salesforce data model (objects, fields, records), can navigate Setup, and have opened Flow Builder before. You don't need to write Apex. Resources matter most in autolaunched and schedule-triggered flows. No user is present to type values, so every value comes from a caller, a query, or a calculation.
What a resource is
A resource is a named value or container that you create in the Manager tab of Flow Builder. Elements such as Assignment, Decision, and Create Records refer to resources by name. You reference them with merge syntax, for example {!varAccountId}.
The resource types you will use most are:
| Resource | Holds | Changes at run time? |
|---|---|---|
| Variable | One value of a single data type | Yes |
| Record variable | One record of a chosen object | Yes |
| Collection variable | An ordered list of values or records | Yes |
| Constant | One fixed value | No |
| Formula | A value calculated each time it is read | Recalculated on read |
| Text template | Text with merge fields | Recalculated on read |
Variables
A variable stores one value. When you create it, you choose a data type: Text, Number, Currency, Boolean, Date, Date/Time, Picklist, Multipicklist, Record, or Apex-Defined. For Number and Currency you also set decimal places.
Each variable has an availability setting. This is the setting that makes the variable useful in autolaunched flows:
- Available for input lets a caller set the variable when it starts the flow.
- Available for output lets the caller read the variable after the flow ends.
A variable with neither box checked is private to the flow. Use private variables for working values such as counters and flags.
Naming matters because merge fields show only the name. A common convention is a short prefix that states the kind of resource, such as var, col, rec, const, and f. Pick one convention and keep it.
Default values and null
A variable with no default value is null until something assigns to it. Null is not the same as an empty string or zero. A Decision outcome that compares a Number variable with 0 does not match a null value. Test for null with the operator Is Null set to True.
Record variables
A record variable holds one record of one object, for example an Account. You read a field with dot syntax: {!recAccount.Name}. You can set a field in an Assignment element and then save the whole record with Update Records.
Get Records can also store its result in individual fields. Choose "Choose fields and let Salesforce do the rest" or select specific fields, and store only what you need. Fewer fields means less memory use.
Collections
A collection variable is a list. You create one by creating a variable and checking Allow multiple values (collection). A collection of records is the most common kind in scheduled flows, because the start element or a Get Records element returns many records.
Three facts about collections shape how you design a flow:
- A collection has an order, and a Loop processes items in that order.
- A collection holds one type only: all Text values, or all records of one object.
- You cannot read item 3 directly by index in the builder. You process items with a Loop, or you use a Collection Filter or Collection Sort element.
Building a collection
The standard pattern has three parts:
- Create an empty collection variable, for example
colCasesToClose. - Inside a Loop, use a Decision to pick the items you want. Then use an Assignment to add the loop item to the collection with the Add operator.
- After the Loop ends, pass the whole collection to a single Update Records or Delete Records element.
The next diagram shows this pattern.
Saving once, outside the Loop, keeps the flow within governor limits. A later chapter covers those limits in detail. For now, remember the rule: never put Create, Update, or Delete Records inside a Loop.
Working on an item
The Loop element exposes the current item as a special variable, for example {!Loop_Cases}. Do not change that loop variable and expect the collection to change. Changing the loop variable alone does not change the collection. The reliable pattern is to copy the item into a separate record variable, set its fields there, and add that record variable to the output collection.
Formulas
A formula resource calculates a value from other resources, using the same syntax as formula fields. A formula is read-only. Flow evaluates it each time an element reads it, so it always reflects the current values of the resources it uses.
Choose a formula data type that matches how you use the result: Boolean, Currency, Date, Date/Time, Number, or Text. This example is a Boolean formula that is true for opportunities that are open and past due:
AND(
NOT({!recOpportunity.IsClosed}),
{!recOpportunity.CloseDate} < TODAY()
)Use a Boolean formula in a Decision outcome by setting the outcome condition to the formula resource equal to {!$GlobalConstant.True}.
This Date formula gives a cutoff 30 days before today. It works well as a filter value in Get Records, because the filter then follows the day the flow runs:
TODAY() - 30Global variables and system values
Flow gives you global values that need no setup:
{!$Flow.CurrentDateTime}and{!$Flow.CurrentDate}give the time the flow runs.{!$Record}is the triggering record in a schedule-triggered or record-triggered flow.{!$Organization.Id}and{!$User.Id}give the org and the running user.{!$GlobalConstant.True},{!$GlobalConstant.False}, and{!$GlobalConstant.EmptyString}give fixed values.
In a schedule-triggered flow, the start element selects records. Each selected record becomes {!$Record} for one run of the flow, so you can read its fields without a Get Records element.
Formula limits to know
- A formula cannot call Apex or query records. It sees only values already in the flow.
- A formula has a compiled size limit, like formula fields. If a long formula fails to save, break it into several smaller formula resources.
- Date and time math follows the running user's time zone settings in many cases. Test with the user the flow actually runs as.
Constants and text templates
A constant holds one value that never changes while the flow runs. Use it for a threshold, such as constMaxAgeDays set to 30. You change it in one place, and every element that uses it updates.
A text template builds a message from merge fields. Choose plain text or rich text. For example, a template used as an email body could read: Case {!recCase.CaseNumber} was closed on {!$Flow.CurrentDate}.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Forgetting to check Available for input | The caller cannot set the variable, and it stays null | Edit the variable and select the option |
| Using a single record variable where a collection is needed | Only one record is processed | Use a collection variable |
| Comparing a possibly null value without a null check | Decision outcome does not match as expected | Add an Is Null check first |
| Reusing a record variable without resetting it | Old field values leak into new items | Assign it at the start of each iteration |
| Hard-coding a number in many elements | Changes need many edits | Create a constant |
Exercises
Exercise
A flow loops over 200 cases and must set Status to Closed on those older than 30 days. Describe where you would place the Update Records element and why.
Show answer
Place it after the Loop, once. Inside the Loop, assign each qualifying case to a record variable, set its Status, and add it to a collection with the Add operator. After the Loop ends, one Update Records element saves the whole collection. This uses one DML statement instead of up to 200, which stays within governor limits.
Exercise
Write a formula expression that returns the date 90 days before the day the flow runs, and name one place in a scheduled flow where you would use it.
Show answer
TODAY() - 90 (a Date formula). Use it as the filter value in a Get Records element, or in a Decision outcome, for example to match records with a CreatedDate before that cutoff. Because it uses TODAY(), the cutoff moves forward each day the flow runs.
Exercise
An Apex class starts your autolaunched flow and passes a value for the variable varAccountId, but the flow behaves as if the value is null. What is the most likely cause?
Show answer
The variable is not marked Available for input, so the caller's value is ignored. Edit the variable in Flow Builder, select Available for input, save, and activate the new version.