Sub topic 6
Passing Data In and Out with Input and Output Variables
This chapter assumes you can build a basic flow in Flow Builder and know the variable data types (Text, Number, Record, Collection). It covers how an autolaunched flow receives data and returns results.
Why variables are the flow's interface
An autolaunched flow has no trigger and no screen. It cannot read a record that "started" it. Any data it needs must be passed in, and any result must be passed out. Two checkboxes on a variable control this:
- Available for input: the caller can set this variable before the flow starts.
- Available for output: the caller can read this variable after the flow finishes.
A variable with neither box checked is private to the flow. Treat the checked variables as the flow's public contract, much like method parameters and return values.
Building the contract
The example flow, Close_Stale_Cases, closes old cases for one account and reports how many it closed. It needs two variables.
| Variable | Data type | Input | Output | Purpose |
|---|---|---|---|---|
| `accountId` | Text | Yes | No | The account whose cases are processed |
| `closedCount` | Number (0 decimal places) | No | Yes | How many cases were closed |
To create them in Flow Builder:
- Open the Manager tab and choose New Resource.
- Set Resource Type to Variable and enter the API name
accountId. - Choose the data type, then check Available for input.
- Repeat for
closedCount, checking Available for output instead.
Inside the flow, a Get Records element finds the stale cases using {!accountId} in its filter. A Loop and an Assignment element add 1 to closedCount for each case.
Common errors
- Forgetting a checkbox. If a variable is not marked for input, the caller cannot set it. Apex and REST calls that pass it fail or are ignored, and a Subflow element will not list it.
- Output set too late. Output variables hold their value only if an Assignment sets them on every path, including the path where nothing matched. Initialize numbers with a default value of 0.
- Type mismatch. Passing a string where the flow expects a Number or Record variable produces a runtime error.
- Name drift. Callers refer to variables by API name. Renaming
accountIdin a new version breaks existing Apex and REST callers.
Collections and records
Variables can hold a Record or a Collection. Mark a collection variable for input to process many records in one run, which also supports bulk-safe design: one flow call can handle a whole list instead of one record at a time. Mark a collection for output to hand back the records the flow created or changed.
Exercise
A flow has a Number variable closedCount that is not marked "Available for output". An Apex class calls interview.getVariableValue('closedCount'). What happens, and how do you fix it?
Show answer
The variable is private to the flow, so Apex cannot read it. The call fails or returns no value. Open the variable in Flow Builder, check "Available for output", save, and activate the new version. Then the Apex class can read the value.
Exercise
Which two settings make accountId a required part of the caller's contract, and why should you give closedCount a default value of 0?
Show answer
Checking "Available for input" lets the caller set accountId. The flow's logic should then use it in its filter. The default of 0 makes sure the caller gets a valid number even if no cases matched and no Assignment ran.