Sub topic 6
Passing data in and out with input and output variables
This chapter assumes you can open Flow Builder, create a variable resource, and read simple Apex. It covers how an autolaunched flow receives data and returns results, and the errors people hit most often.
Why variables are the flow's interface
An autolaunched flow has no trigger, no screen, and no record context. Whatever it needs must be handed to it. Whatever it computes must be handed back. Two settings on a resource control this:
- Available for input: the caller can set the variable's starting value.
- Available for output: the caller can read the variable's final value after the flow finishes.
You set both in the variable's resource dialog under Advanced (shown as "Input and Output Options"). A variable with neither box checked is private to the flow. This makes the checked variables the flow's public interface, much like method parameters and a return value.
| Input box | Output box | Behavior |
|---|---|---|
| Off | Off | Private working variable. |
| On | Off | Caller sets it. Caller cannot read it back. |
| Off | On | Flow sets it. Caller reads it. |
| On | On | Caller sets the starting value and reads the final value. |
Which resource types can be inputs and outputs
Variables of every data type, including record variables and collection variables, can be marked for input or output. Constants, formulas, and text templates cannot. If you need to pass a computed value out, assign the formula's result to a variable first.
Pick the data type to match what the caller will send:
- Text, Number, Currency, Date, Date/Time, Boolean for simple values.
- Record for one sObject, such as an Account.
- Record collection for a list of sObjects.
- Apex-defined types, when the caller is Apex and the flow uses an
@InvocableVariableclass.
For a Number variable, set Decimal Places. A value of 0 gives an integer in Apex, which matters for casts later in this chapter.
Worked example: Close_Stale_Cases
Suppose a flow named Close_Stale_Cases closes open cases on one account and reports how many it closed. The flow needs these resources:
| API name | Type | Input | Output | Purpose |
|---|---|---|---|---|
| `accountId` | Text | Yes | No | The account to process. |
| `closedCount` | Number (0 decimals) | No | Yes | Count of cases closed. |
| `staleCases` | Record collection (Case) | No | No | Working list. |
The logic runs in this order:
- Get Records on Case. Filter
AccountIdequals{!accountId}andIsClosedequals false. Store all records instaleCases. - Loop over
staleCases. For each case, setStatusto Closed and add it to a second collection,casesToUpdate. Add 1 toclosedCountwith an Assignment. - Update Records on
casesToUpdate, after the loop.
Keep the Update outside the loop. One Update on a collection uses one DML statement, while an Update inside the loop uses one per record and hits the 150 DML statement limit quickly.
Initialize closedCount with a default value of 0, or set it with an Assignment before the loop. If you skip this and no cases match, the caller may receive null instead of 0.
Calling the flow from another flow
In a parent flow, add a Subflow element and choose Close_Stale_Cases. Flow Builder lists the child's input variables and lets you set each with a value or a resource. Output variables appear as values you can store into parent variables.
- Map
accountIdto a parent resource such as{!$Record.Id}or a record variable's Id. - Under the output section, store
closedCountinto a parent variable, for exampleparentClosedCount. - Use
parentClosedCountin a later Decision or in an email text template.
The subflow runs in the same transaction as the parent. The parent waits for it to finish, then continues with the returned values. The next chapter covers Subflow in more detail.
Calling the flow from Apex
Apex passes inputs in a map and reads outputs with getVariableValue:
Map<String, Object> inputs = new Map<String, Object>();
inputs.put('accountId', '001000000000000AAA');
Flow.Interview.Close_Stale_Cases interview = new Flow.Interview.Close_Stale_Cases(inputs);
interview.start();
Integer closedCount = (Integer) interview.getVariableValue('closedCount');
System.debug('Cases closed: ' + closedCount);Points to check:
- Map keys are the variable API names and are case-sensitive in practice. Match them exactly.
- Only variables marked for input are accepted. Passing a key for a private variable does not set it.
getVariableValuereturnsObject. Cast it to the type you expect. A Number variable with decimal places greater than 0 returns aDecimal, so casting it toIntegerfails.getVariableValuereturns only variables marked for output. Asking for a private variable returns null.- The flow must be active, or the call uses the latest version only if you have permission to run inactive flows. Do not rely on that in production.
Calling the flow from the REST API
The REST API exposes the active flow as an invocable action. The API name goes in the path and the inputs go in the body:
POST /services/data/v60.0/actions/custom/flow/Close_Stale_Cases
Authorization: Bearer <access_token>
Content-Type: application/json
{
"inputs": [
{ "accountId": "001000000000000AAA" }
]
}The response is a JSON array with one entry per element of inputs. Each entry has an outputValues object that holds the output variables, such as closedCount, plus isSuccess and an errors list. Check isSuccess for each entry. A request can return HTTP 200 while one of its entries failed.
Each object in the inputs array is one run of the flow. Sending several objects runs the flow in bulk, so the flow must follow bulk-safe design (see the later chapter on governor limits).
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Subflow element does not list a variable | Variable not marked Available for input or output | Open the child flow, edit the variable, check the box, save as a new version, activate. |
| Apex returns null for an output | Variable is private, name is misspelled, or the flow never assigned it | Check the output box, the spelling, and the default value. |
| `System.TypeException` on cast | Number decimal places do not match the Apex type | Cast to `Decimal`, or set decimal places to 0. |
| Caller still sees old behavior | New version saved but not activated | Activate the new version. |
| Record variable is empty in the child | Parent passed an unset record variable | Populate it in the parent before the Subflow call. |
Design guidance
- Name inputs and outputs for the caller, not for the flow's internals.
accountIdis clearer thanvarText1. - Keep the interface small. Every public variable is a contract that callers depend on.
- Prefer passing record Ids over whole records when the child only needs to query. Pass records when the child should work on values the parent just changed and not yet saved.
- Changing a variable's name or type in a new version can break existing Apex and REST callers. Adding a new optional input is safer.
- Output values reflect the flow's state when it finishes. If a fault path ends the flow early, outputs hold whatever they were assigned up to that point.
Exercise
A flow has a Number variable total with 2 decimal places, marked Available for output. An Apex caller runs Integer t = (Integer) interview.getVariableValue('total'); and gets a TypeException. What is the cause and what are two ways to fix it?
Show answer
With decimal places greater than 0, the variable comes back as a Decimal, and an Object holding a Decimal cannot be cast to Integer. Fix it by casting to Decimal (Decimal t = (Decimal) interview.getVariableValue('total');), or by setting the variable to 0 decimal places in a new active version if only whole numbers are needed.
Exercise
A parent flow adds a Subflow element for Close_Stale_Cases, but the child's closedCount does not appear as an output. List the steps to fix it.
Show answer
Open the child flow and edit the closedCount variable. Check Available for output. Save, then activate the new version. Reopen the parent flow and the Subflow element so it refreshes the child's interface, then map the output to a parent variable.