Introduction
Subflows are modular, reusable Flow components in Salesforce that let you encapsulate common logic into a separate Flow and invoke it from other Flows. They improve maintainability, reduce duplication, and make large automation projects easier to manage.
Why use Subflows?
Subflows help with:
- Reusability: centralize repeated logic (e.g., record creation, validation, calculations)
- Maintainability: update one subflow instead of changing many flows
- Readability: keep parent flows focused on orchestration while subflows handle discrete tasks
- Separation of concerns: divide complex automations into smaller units
When to use a Subflow
Consider a subflow when logic is:
- Used by multiple flows
- Large or complex enough to reduce parent flow readability
- Requires independent testing or versioning
How to implement Subflows in a Flow
1. Design the subflow
Create a new flow to encapsulate the reusable logic. Typically a subflow should be an autolaunched flow (no trigger screen) so it can be invoked programmatically by other flows.
2. Define input and output variables
Expose only the variables that the parent flow needs to pass in or receive back. Mark variables as available for input and/or output.
<!-- Example: define variables in the subflow UI -->
// accountId (Text) — Available for input
// needContactCreation (Boolean) — Available for input
// createdContactId (Text) — Available for output
3. Implement the subflow logic
Build the steps inside the subflow: Get Records, Create Records, Update Records, Decisions, Assignment, or call Apex if needed. Keep responsibilities narrow.
4. Save and Activate the subflow
Activate the specific version you want parent flows to call. Remember: flows call a specific version. You can update parent flows to call the new version when ready.
5. Call the subflow from a parent flow
In the parent flow (screen flow, record-triggered flow, or autolaunched flow), add the Subflow element. Choose the subflow by name and map the variables between the parent flow and the subflow.
<!-- Example mapping in the Subflow element -->
// Subflow input: accountId <-- Parent variable: recordId
// Subflow input: needContactCreation <-- Parent variable: createContactFlag
// Subflow output: createdContactId --> Parent variable: newContactId
6. Handle errors and bulkification
If the parent flow runs in bulk (e.g., record-triggered flow on multiple records), ensure the subflow logic is bulk-safe. Salesforce executes subflows synchronously in the same transaction as the parent flow, so avoid DML inside loops and follow governor limits. Also consider fault paths on the Subflow element to capture and route errors.
Best practices and considerations
Versioning
Flows are versioned. When you activate a new subflow version, parent flows continue to call the version they referenced unless you update the parent flow to call another active version. Plan versioning and release to avoid unexpected behavior.
Governor Limits and Transaction Boundaries
Subflows run in the same transaction as the caller. They share governor limits (SOQL, DML, CPU). Design for bulk operations and keep transactions efficient. For long-running or async work, consider using Platform Events, Queueable Apex, or scheduled paths instead of heavy synchronous subflows.
Testing and Debugging
Use the Flow Debugger to step through parent and subflow execution. Test each subflow independently with representative inputs, and validate how output variables are returned to the caller.
Security and Sharing
Flows respect the running user’s permissions. If a subflow performs record operations, ensure the running user has appropriate CRUD/FLS or run it in system context (e.g., an invocable Apex action or a flow launched by a system-level process) when necessary.
Quick example scenario
Use case: Every time an Account is created, you need to create a default Contact and a related Task. Instead of repeating creation steps across multiple flows, create a subflow named “CreateAccountChildRecords” with inputs: accountId (Text) and createContact (Boolean). Outputs: contactId (Text).
Parent record-triggered flow (on Account create) calls the subflow, passing {!record.Id} & {!CreateContactFlag}. The subflow creates the Contact and Task, sets the output {!contactId}, which the parent flow can use for further processing (e.g., send email).
Summary
Subflows are a powerful pattern in Salesforce Flow to encapsulate reusable logic, improve maintainability, and simplify complex automations. Implement them as autolaunched flows, expose clear input/output variables, map data in the Subflow element, and pay attention to versioning and governor limits.








Leave a Reply