Sub topic 7
Reusing logic with Subflow
This chapter assumes you can build a basic flow in Flow Builder. It also assumes you know what input and output variables are, as covered in the previous chapter. Here you will call one autolaunched flow from another by using the Subflow element.
Why use a subflow
A subflow is a flow that another flow calls. The calling flow is the parent. The called flow is the child, and it must be an active autolaunched flow. Screen flows and other flow types can also be called, but this chapter focuses on autolaunched children.
Subflows let you write logic once and use it in many places. A scheduled flow and a record-triggered flow can both call the same "Close stale cases" child. When the rule changes, you edit one flow.
How the call works
The parent adds a Subflow element and picks the child by name. Flow Builder then lists the child's input and output variables.
- Each variable marked Available for input appears as a field you can set. Map a value from the parent, such as a record ID or a variable.
- Each variable marked Available for output can be stored in a parent variable after the child finishes.
- The parent waits until the child ends. Then it continues along the connector from the Subflow element.
The child shares the transaction of the parent. The two flows count against the same governor limits. Calling a subflow does not give you a fresh set of limits.
Worked example
Suppose the child flow Close_Stale_Cases has two variables:
accountIdis a Text variable, available for input.closedCountis a Number variable, available for output.
In the parent, you add a Subflow element and set accountId to the ID of the current record. You store closedCount in a parent variable named casesClosed. A later Decision element can then check whether casesClosed is greater than zero before it sends a notification.
Common errors
- The child does not appear in the list. It is probably inactive, or it is not an autolaunched flow. Activate it first.
- A variable is missing from the mapping. The variable is not marked for input or output. Open the child, change its availability, and save a new version.
- The parent keeps using old behavior. A Subflow element calls the active version of the child by default. Activate the new child version.
- Limit errors in large runs. A subflow placed inside a Loop runs once per item. Each run may do its own queries or DML. Pass a collection into the child and do the work once.
Exercise
A parent flow calls a child flow inside a Loop, once per record, and the child runs a Get Records element. Why can this hit governor limits, and how would you fix it?
Show answer
The child shares the parent's transaction, so every call adds another query to the same limit count. Many iterations can exceed the SOQL query limit. Fix it by passing the whole collection into the child as an input variable and running the query once, outside the loop.