Sub topic 7
Reusing logic with Subflow
This chapter assumes you can build a basic flow in Flow Builder. It also assumes you know how input and output variables work, which the previous chapter covered. Here you use the Subflow element to call one flow from another, pass data across the boundary, and avoid the usual mistakes.
What a Subflow does
A Subflow is an element that runs another flow in the middle of the current one. The calling flow is the parent. The flow it calls is the child. The parent pauses at the Subflow element, the child runs to completion, and the parent continues from the next element.
A typical use is a rule you would otherwise copy into several flows. Examples are "close stale cases for an account", "calculate a renewal date", or "send a standard notification". You build it once as an autolaunched flow and call it from a schedule-triggered flow, a record-triggered flow, or another autolaunched flow.
The working model
Three rules explain most Subflow behavior.
- Same transaction. The child runs inside the parent's transaction. Queries, DML statements, and CPU time in the child count against the same governor limits as the parent. A Subflow gives you reuse, not a fresh set of limits.
- Variables are the only interface. The child sees only the values the parent passes to its input variables. The parent sees only the child's variables marked Available for output. The two flows cannot read each other's other resources.
- Active version. The parent calls the child by its API name and runs the child's active version. Treat activation as a deployment step for the child, because every parent that calls it picks up the change.
Building the call
Suppose you have an autolaunched flow named Close_Stale_Cases. It has an input variable accountId (Text) and an output variable closedCount (Number, 0 decimal places). To call it:
- In the parent flow, add a Subflow element from the Add Element menu.
- Search for and select
Close_Stale_Cases. Only flows that can be run as subflows appear in the list. - Under Set Input Values, map
accountIdto a value in the parent. This can be a variable, a loop item field such as{!Loop_Accounts.Id}, or a formula. - Under Store Output Values, assign
closedCountto a variable in the parent, such astotalClosed. - Connect the element, and add a fault connector if you need to handle child failures.
In the flow's metadata, the element looks like this:
<subflows>
<name>Close_Stale_Cases_For_Account</name>
<label>Close Stale Cases For Account</label>
<locationX>176</locationX>
<locationY>287</locationY>
<connector>
<targetReference>Add_To_Total</targetReference>
</connector>
<flowName>Close_Stale_Cases</flowName>
<inputAssignments>
<name>accountId</name>
<value>
<elementReference>Loop_Accounts.Id</elementReference>
</value>
</inputAssignments>
<outputAssignments>
<assignToReference>closedCount</assignToReference>
<name>closedCount</name>
</outputAssignments>
</subflows>flowName is the child's API name. Each inputAssignments entry names a child variable and the parent value that feeds it. Each outputAssignments entry copies a child variable into a parent variable. Here assignToReference is the parent variable, and it also happens to be called closedCount.
Matching variables
The mapping fails to save or run correctly when the two sides do not match. Check these points:
| Check | What to look for |
|---|---|
| Data type | Text to Text, Number to Number. Flow does not convert types in the mapping. |
| Collection setting | A single-value child variable cannot take a collection, and the reverse. |
| Object type | A record variable must use the same object on both sides. |
| Input flag | The child variable must be *Available for input* to appear under input values. |
| Output flag | The child variable must be *Available for output* to appear under output values. |
| Number scale | Decimal places on Number variables should be compatible. |
Subflows and bulk behavior
The most common design error is calling a Subflow once per record inside a Loop, where the child runs a Get Records or an Update Records element. Each pass does its own query or DML, and a few hundred records can exceed the limits of 100 SOQL queries or 150 DML statements in one transaction.
Prefer to pass a collection into the child and let the child do the work in bulk. For example, give the child an input variable casesToClose that is a record collection of Case. The child updates the whole collection with one Update Records element. The parent calls it once, after its own loop has built the collection with Assignment elements.
This matters most for schedule-triggered flows. The platform batches the selected records, and each batch shares limits across the interviews it runs. A child flow that is fine for one record can fail when 200 records arrive together.
Errors and fault handling
An unhandled error in the child stops the child and surfaces as a failure in the parent. The error message names the child element that failed, so open the child to find it.
Two practices keep failures manageable:
- Put fault paths on the DML and query elements inside the child. Have the child set an output variable such as
errorMessage(Text) andsuccess(Boolean), then end normally. - In the parent, read those outputs after the Subflow element and decide what to do. A Decision on
successcan send a notification or log a record.
This turns an opaque failure into data the parent can act on. If you prefer the parent to handle everything, add a fault connector to the Subflow element itself.
Versioning and change control
Because the parent runs whatever version of the child is active, a change to the child affects every caller as soon as you activate it. Some habits reduce the risk:
- Do not rename or delete input and output variables on an active child. Callers that reference them will break. Add a new variable instead.
- Keep a short list of parents in the child's description.
- Test the child with the Debug option before activating a new version, using realistic inputs.
- Use clear API names. The name is the contract, and the parent stores it as text in
flowName.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Child flow is missing from the Subflow search | The flow is not the right type, or has no saved version | Check the flow type and save or activate the child |
| An input variable is not offered in the mapping | *Available for input* is off | Edit the child variable and turn it on |
| Output value is always empty | *Available for output* is off, or the child never assigns it | Turn it on and trace the child's path |
| Limit error such as "Too many SOQL queries: 101" | Subflow called inside a Loop with a query in the child | Pass a collection and process it once |
| Parent breaks after a child change | A variable was renamed or its type changed | Restore the variable and add a new one |
Exercise
A schedule-triggered flow loops over 300 Opportunity records and, for each one, calls a Subflow that runs Get Records on Contact and then Update Records on the opportunity. The run fails with a governor limit error. Why does it fail, and how would you redesign it?
Show answer
The child runs in the same transaction as the parent, so its query and DML count once per loop iteration. With 300 records that goes past the limit of 100 SOQL queries (and 150 DML statements). Redesign it so the child does the work in bulk. The parent adds each opportunity to a record collection with an Assignment element inside the loop. After the loop, it calls the Subflow once, passing the collection through an input variable. The child then queries related contacts once and updates the whole collection with a single Update Records element.