Sub topic 8
Starting an Autolaunched Flow from Apex and REST
This chapter assumes you have built an autolaunched flow with input and output variables, and that you can read simple Apex and an HTTP request. An autolaunched flow has no trigger, so code can start it on demand. Apex uses the Flow.Interview class. External systems use the REST API.
Prepare the flow
Both approaches depend on the same flow settings:
- The flow must be an autolaunched flow, and it must be active for REST calls.
- Each variable that code sets must have Available for input checked.
- Each variable that code reads must have Available for output checked.
- Code refers to the flow and its variables by API name, which is case-sensitive.
Start the flow from Apex
Create a Flow.Interview object named after the flow. Pass a map of input variable names and values to the constructor, call start(), and then read outputs with getVariableValue.
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);getVariableValue returns an Object, so you must cast the result. If the output variable is not marked available for output, the call returns null.
The flow runs inside the caller's transaction. Its SOQL queries and DML statements count against the same governor limits as your Apex.
If the flow name is only known at run time, use Flow.Interview.createInterview('Close_Stale_Cases', inputs) instead. It returns a Flow.Interview that you start the same way.
Start the flow from REST
The REST API exposes each active autolaunched flow as a custom invocable action. Send a POST to the flow's API name. The body holds an inputs array with one object per run.
POST /services/data/v60.0/actions/custom/flow/Close_Stale_Cases
Authorization: Bearer <access_token>
Content-Type: application/json
{
"inputs": [
{ "accountId": "001000000000000AAA" }
]
}The response is an array with one result per input object. Each result has isSuccess, an errors list, and an outputValues map holding the flow's output variables.
Common errors
| Symptom | Likely cause |
|---|---|
| Compile error on `Flow.Interview.Name` | The flow name is misspelled or the flow does not exist in this org. |
| Output is `null` | The variable is not marked available for output, or the flow never assigned it. |
| REST returns an error that the flow is not found | The flow has no active version, or the API name is wrong. |
| A required input error | An input variable was left out of the map or the request body. |
Exercise
A developer calls an autolaunched flow from Apex and reads an output variable with getVariableValue, but the result is always null even though the flow runs. Give two likely causes.
Show answer
The output variable is not marked "Available for output" in the flow, or the flow never assigns a value to it on the path that ran. A misspelled variable name can also return null.