Sub topic 2
Reusing Logic with Subflow
Assume you understand Salesforce objects, fields, records, and basic Flow Builder navigation. A Subflow calls one flow from another flow. The called flow is usually an autolaunched flow because it has no screen or trigger.
Reusing logic with Subflow
Put reusable work in an autolaunched flow. Mark its variables as Available for input or Available for output. In the parent flow, add a Subflow element, choose the child flow, and map values between variables.
For example, a parent flow can pass an Account ID to a child flow that finds stale Cases and returns the number closed.
Starting an autolaunched flow from Apex and REST
Apex can start an active flow version and pass input variables. It can then read output variables.
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);An external client can use the REST API. The flow must be active, and the caller needs suitable Salesforce permissions.
POST /services/data/v60.0/actions/custom/flow/Close_Stale_Cases
Authorization: Bearer <access_token>
Content-Type: application/json
{"inputs":[{"accountId":"001000000000000AAA"}]}Schedule-triggered flows: automation on a timer
A schedule-triggered flow starts automatically instead of waiting for another flow or program. It selects records for an object, then processes those records. It is useful for daily cleanup, reminders, or status updates.
Setting the schedule and records
Configure the start element with:
- A start date and time.
- A frequency such as once, daily, or weekly.
- An object, such as Opportunity.
- Filter conditions, such as
StageName = Prospecting.
The flow evaluates those conditions when the scheduled run begins. Avoid assuming that records remain unchanged between scheduling and execution.
For large or complex workloads, scheduled Apex may provide more control over batching and transaction behavior.
Exercise
Which flow type should you use for reusable logic called by another flow?
Show answer
Use an autolaunched flow, then call it with a Subflow element.