Sub topic 5
Autolaunched flows: reusable automation with no trigger
This chapter covers what an autolaunched flow is, how it starts, and how to build one. It assumes you know the Salesforce data model (objects, fields, records), can find your way around Setup, and have seen Flow Builder at least once. The Apex examples are optional reading.
What an autolaunched flow is
An autolaunched flow is a flow with no trigger of its own. It does not start when a record changes, when a user clicks a button on a screen, or when a clock reaches a set time. It runs only when something else starts it. It also has no screens, so it cannot ask a user for input while it runs.
Other flow types have a built-in way to start:
| Flow type | What starts it | Shows screens |
|---|---|---|
| Screen flow | A user opens it (button, tab, page component) | Yes |
| Record-triggered flow | A record is created, updated, or deleted | No |
| Schedule-triggered flow | A set date, time, and frequency | No |
| Autolaunched flow (no trigger) | Another flow, Apex, REST API, or another caller | No |
Because it has no trigger, an autolaunched flow works like a function in code. You write the logic once and call it from many places.
In Flow Builder you create one by choosing Autolaunched Flow (the "no trigger" option) in the New Flow dialog.
Where the flow can be called from
An active autolaunched flow can be started from these places:
- Another flow, through the Subflow element (covered in a later chapter).
- Apex, through the
Flow.Interviewclass. - The REST API, through the invocable actions endpoint.
- Other tools, such as Process Builder (a legacy tool), Orchestrator, and flow actions on some page and button configurations.
Building a flow that closes stale cases
The running example for this chapter is a flow named Close_Stale_Cases. Given an account, it finds that account's open cases that have not been modified in 30 days, closes them, and returns how many it closed.
Step 1: Create the resources
Create these resources in the Toolbox before adding elements:
- A Text variable
accountId, with Available for input checked. - A Number variable
closedCount(0 decimal places, default value 0), with Available for output checked. - A formula resource
StaleCutoffof type Date/Time with the formulaNOW() - 30. - A Record (Collection) variable
staleCasesof object Case.
The input and output checkboxes decide whether callers can see the variables. A variable without them is private to the flow. The next chapter covers these settings in detail.
Step 2: Add the elements
The flow follows this order:
- Get Records on Case. Conditions:
AccountIdequals{!accountId},IsClosedequals false, andLastModifiedDateis less than{!StaleCutoff}. Store all records instaleCases. - Decision: if
staleCasesis empty, end the flow early. - Loop over
staleCases. In the loop, an Assignment setsStatustoClosedon the current item and adds it to a second collection,casesToUpdate. A second Assignment adds 1 toclosedCount. - Update Records on
casesToUpdate, placed after the loop.
The Update Records element sits outside the loop on purpose. The flow gathers all changes in a collection and writes them in one operation. Placing a data element inside a loop runs it once per iteration and quickly hits governor limits, which the bulk chapter covers.
Step 3: Save and activate
Save the flow with the API name Close_Stale_Cases. The API name is how Apex and REST refer to it, so choose it carefully. A flow must be active before most callers can run it. Apex is an exception: it can run the inactive latest version only in specific cases, and relying on that is a poor habit. Activation and versions have their own chapter.
Calling the flow from Apex
Apex starts an autolaunched flow by creating a Flow.Interview object named after the flow, passing a map of input variables, and calling start().
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 note:
- The map keys must match the variable API names exactly, including case.
getVariableValuereturnsObject, so you cast it. It works only for variables marked Available for output.- The flow runs in the same transaction as the Apex code that called it. They share governor limits.
Calling the flow from the REST API
The REST API runs the flow through the custom invocable actions endpoint. The flow's API name goes in the path and the input variables 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 inputs array holds one object per run. Each object contains the input variables for that run. The response is a JSON array with one result per input, and each result includes the flow's output variables, such as closedCount.
Run context
How an autolaunched flow runs depends on who starts it:
- From another flow, it inherits the context of the parent flow.
- From Apex, it runs in system context by default, so field-level security and sharing are not enforced unless the flow's settings say otherwise.
- From REST, it runs as the authenticated API user.
A later chapter covers this in detail. For now, remember that the same flow can behave differently depending on its caller, so test each entry point.
Common errors
- Flow not found or not active. The API name is misspelled, or the flow has no active version.
- Input not accepted. The variable is not marked Available for input, or the data type does not match what the caller sent.
- Output is null in Apex. The variable is not marked Available for output, or the flow never assigned it.
- Governor limit exceeded. A data element sits inside a loop, or the caller already used most of the transaction's limits.
- Unhandled fault. A Get, Update, or other data element fails and no fault path exists. The whole interview stops and rolls back.
Exercise
A developer calls Close_Stale_Cases from Apex and gets null from getVariableValue('closedCount'), even though cases were closed. What is the most likely cause?
Show answer
The closedCount variable is not marked Available for output, so Apex cannot read it. Marking the checkbox and saving a new version, then activating it, fixes the problem.
Exercise
Why is the Update Records element placed after the loop in this flow, and what would go wrong if it were inside?
Show answer
Placing it after the loop lets the flow update all cases in one DML operation, using the casesToUpdate collection. Inside the loop, it would run once per case. A large account would exceed the limit of 150 DML statements per transaction and the flow would fail.
Exercise
Name three callers that can start an autolaunched flow, and say how each one passes input.
Show answer
A parent flow passes input through the Subflow element's input assignments. Apex passes a map of variable names to values when it constructs Flow.Interview. The REST API passes an inputs array in the JSON body of a POST to the invocable actions endpoint.