Sub topic 7
Calling the Method from Flow Builder
This chapter assumes you can write an @InvocableMethod with request and response classes, and that you know the basics of Flow Builder: variables, record-triggered flows, and elements. Here you will take the AccountTaskCreator class and call it from a flow.
How Apex appears in Flow Builder
Flow Builder lists each invocable method as an Action element. In the Add Element dialog, choose Action, then search for the method's label. The category value on the annotation groups it in the left-hand list. The AccountTaskCreator method appears as "Create Follow-up Tasks" under the "Account" category.
Only methods that follow the signature rules show up. If your method is missing, check that the class is public or global, that the method is static, and that the class has only one @InvocableMethod. Also check that the class has been saved and deployed.
Mapping inputs
After you select the action, Flow Builder reads the @InvocableVariable fields of the Request class and shows each one as an input.
| Apex field | Flow input label | Behavior |
|---|---|---|
| `accountId` | Account Id | Required, because `required=true` |
| `subject` | Task Subject | Optional, and the Apex code defaults it to "Follow up" |
For each input, you can set a value in one of two ways:
- Enter a literal value or pick a flow resource, such as
{!$Record.Id}in a record-triggered flow. - Leave the input empty. Flow passes
null, so your Apex code must handle it. TheString.isBlankcheck increateTasksdoes this forsubject.
If your method takes a List<Request>, the Flow Builder panel shows one set of inputs. The flow does not ask you to build a list yourself. Each flow interview supplies one Request, and the platform groups requests from many interviews into the list your method receives. The bulkification chapter covers why this matters.
Using outputs
The Result class defines the outputs. In the action's Advanced settings, select Manually assign variables. This lets you store only the outputs you need.
- Open the action and expand Advanced.
- Select Manually assign variables.
- Map
taskIdto a flow variable of type Text, or to a record variable if the output is a record. - Map
isSuccessto a Boolean variable. - Map
errorMessageto a Text variable.
Later elements can read these variables. For example, a Decision element can check whether isSuccess is false and send the flow down a path that shows an error on a screen or logs it.
Without manual assignment, Flow Builder keeps the outputs in the action's own output reference, such as {!Create_Follow_up_Tasks.isSuccess}. Either approach works. Manual assignment gives you clearer variable names.
Fault connectors
An unhandled exception in your Apex code does not stop the whole flow. If the action has a Fault connector, the flow follows it, and {!$Flow.FaultMessage} holds the exception message. Without a fault connector, the flow fails and the user sees an error, or an admin receives a flow error email.
Per-record results and fault connectors do different jobs:
- Use the result fields for expected failures, such as a validation rule rejecting one record.
- Use the fault connector for unexpected failures, such as an exception thrown for the whole batch.
The call sequence
The diagram shows how a flow and the Apex method exchange data.
Practical checks before you activate
- Test the flow in the Flow Builder debug mode with real records.
- Confirm that a blank
subjectproduces the "Follow up" default. - Rename the action element with a clear API name, because later elements refer to its outputs by that name.
- Re-open the action after changing the Apex class. Flow Builder can keep stale input mappings if you rename or remove an
@InvocableVariablefield.
Exercise
A record-triggered flow calls your invocable method, but the action does not appear in the Action list. Name three things to check in the Apex class.
Show answer
Check that the class is public or global, that the method is static and annotated with @InvocableMethod, and that the class has only one @InvocableMethod. Also confirm the class is saved and deployed to the org you are using.
Exercise
You want a Decision element to react when one task fails to insert. Which output should it use, and which connector handles an exception thrown for the whole call?
Show answer
The Decision should check the isSuccess output, and errorMessage can supply the text. The Fault connector on the action handles an unhandled exception.