Sub topic 7
Calling the method from Flow Builder
This chapter assumes you can write an invocable method with request and response classes, as covered in earlier chapters. It also assumes you know how to create a flow and add elements in Flow Builder. Here you will see how the Apex code shows up in the builder, how to map inputs and outputs, and what changes between flow types.
From annotations to the Apex action
Flow Builder discovers invocable methods when you add an Action element. It reads the annotations on your class, so every label and description you wrote in Apex appears in the builder.
Each annotation maps to something visible in the UI:
| Apex source | Where it appears in Flow Builder |
|---|---|
| `@InvocableMethod(label=...)` | The action name in the Action picker |
| `category=...` | The group heading the action is listed under |
| `description=...` | Help text shown when you select the action |
| `@InvocableVariable(label=...)` | The name of an input or output field |
| `required=true` | The input is marked required and must be mapped |
| `description` on a variable | Help text beside that field |
Names and labels matter more than they seem. A flow builder, who may not read Apex, sees only these labels. A label such as Create Follow-up Tasks with the category Account is far easier to find than doWork.
Adding the action to a flow
Follow these steps to call the AccountTaskCreator method from earlier in this tutorial:
- Open a flow in Flow Builder and click the + icon on the canvas.
- Choose Action.
- In the search box, filter by the Apex type, then search for
Create Follow-up Tasks. You can also browse by the category you set. - Select the action. The builder shows its input fields and a label and API name for this use.
- Give the element a clear label, for example
Create Follow-up Task. The API name becomes the prefix you use to reference its outputs, such as{!Create_Follow_up_Task}.
Only classes with a valid @InvocableMethod appear in this list. If your class does not appear, check that it compiles, that the method is public static or global static, and that it is the only invocable method in the class. A deploy that fails or a class that does not compile is the most common cause.
Mapping inputs
When your method takes a List<Request>, the builder shows each @InvocableVariable field in Request as a separate input. You do not build a Request object yourself. The flow supplies one Request per interview, and Salesforce collects them into the list your method receives.
For the task creator, the inputs are:
- Account Id is required. Map it to a record variable's Id, for example
{!$Record.Id}in a record-triggered flow. - Task Subject is optional. Map it to text, such as a literal
Call about renewal, or leave it empty. Because the Apex code checksString.isBlank, an empty value falls back toFollow up.
Each input accepts a literal value or a reference to a resource such as a variable, a formula, or a field on a record. The data type must be compatible. If you declare an input as Date, the flow must supply a date and not a date/time. If you declare List<String>, the flow must supply a text collection. If the type does not match, the builder shows a validation error when you save.
Required inputs and nulls
required=true is enforced by the builder, not by the Apex runtime. A required input must be mapped to something, but the value it resolves to can still be null. For example, you can map a required Id to a variable that happens to be empty at run time. Your method should still check for null values for anything it cannot work without.
Inputs that are collections
If a variable inside Request is itself a list, such as List<Id> contactIds, the flow supplies a collection variable for it. Do not confuse this with the outer list. The outer list is Salesforce batching interviews. The inner list is data a single interview passes in.
Mapping outputs
Outputs are optional. If your method returns List<Result>, the Manage outputs section of the action lets you decide which fields to keep. The default, Use values from earlier in the flow, is not the one you want for outputs. Instead, open the output section and turn on the fields you need, such as Task Id or Is Success.
Once an output is included, you reference it with the element's API name and the field name:
{!Create_Follow_up_Task.taskId}{!Create_Follow_up_Task.isSuccess}{!Create_Follow_up_Task.errorMessage}
You can then use these in later elements. A Decision element can branch on {!Create_Follow_up_Task.isSuccess}. An Assignment element can copy taskId into a variable. A screen can display errorMessage.
You can also store outputs in your own variables. Choose Manually assign variables and pick a flow variable for each field. This is useful when you want to keep a value after later elements overwrite the action's outputs, or when the same action runs inside a loop.
From the flow's point of view, each interview gets back exactly one Result, the one at its own position in your returned list. This is why the earlier chapter on bulkification stressed returning one result per request, in the same order. The flow never sees the whole list.
Behavior in different flow types
The same action behaves differently depending on where it runs.
- Record-triggered flows (after save). When many records change in one transaction, such as a data load of 200 records, Salesforce can batch the interviews. Your method then receives a list longer than one. Before-save record-triggered flows do not support Apex actions, so use an after-save flow.
- Screen flows. One interview runs at a time, so your list normally contains one item. Write the method to handle many anyway, because the same class may be used elsewhere.
- Autolaunched flows. The list size depends on the caller. Called from Apex or a process, it may contain several interviews.
- Scheduled flows. Interviews are processed in batches, so the same bulk rules apply.
A common mistake is to test only a screen flow, see that it works with one item, and ship code that fails on 200 records in a data load.
Fault paths in the builder
When your method throws an unhandled exception, the flow needs somewhere to go. On the action element, you can add a Fault connector, drawn as a red line from the element. If you add one, the flow follows it when the Apex throws. If you do not, the whole flow interview fails and Salesforce can send an error email to the flow's owner.
On the fault path, use the global variable {!$Flow.FaultMessage} to read the error text. A typical fault path has an Assignment or a Create Records element that logs the message, or a screen that shows it to the user.
There is an important distinction between two kinds of failure:
- Thrown exceptions fail the whole call. The flow takes the fault path, and DML earlier in the same transaction is rolled back.
- Per-record results such as
isSuccess = falsedo not trigger the fault path. The action finishes normally and the flow has to check the result with a Decision element.
Your design should pick one on purpose. If one failing record should not stop the others, return per-record results and check them in the flow. If any failure should undo the work, throw an exception and use the fault path.
Keeping Apex and flows in sync
A flow stores a reference to your method and the names of the variables it maps. This creates a coupling that is easy to break.
- Do not rename or remove an
@InvocableVariablefield that an active flow uses. The change can fail to deploy or break the flow. - Adding a new optional input is safe. Existing flows ignore it, and its value is null.
- Adding a new required input can break existing flows that do not map it.
- Changing a type, for example from
StringtoId, is also a breaking change.
When you must change a contract, create a new class or a new variable, move flows to it, and retire the old one after.
Also check access. The user who runs the flow needs access to the Apex class, granted through a profile or a permission set, unless the flow runs in a mode that does not require it. If users report that the action fails for them but works for you as an admin, a missing class permission is a likely cause.
Debugging in the builder
Flow Builder's Debug button runs the flow and shows each element's inputs and outputs. For an Apex action, it displays the values passed in and each field returned, so you can confirm that mapping is correct before activating. For deeper problems, enable a debug log for the running user and look for the FLOW_ELEMENT_BEGIN and USER_DEBUG entries around your Apex call.
Exercise
A record-triggered flow calls your invocable method, and 200 accounts are updated by a data load. What does your method receive, and how does each interview get its answer?
Show answer
The method receives one list with up to 200 Request objects, one per interview. It must return a list of the same size and in the same order. Salesforce hands each interview the Result at its own position, so the result for Request 5 must be at index 5.
Exercise
Your method returns isSuccess = false for one record, but the flow never takes its fault path. Why, and how do you handle the failure in the flow?
Show answer
The fault path is followed only when the method throws an unhandled exception. A returned isSuccess = false is a normal result. Include the output in Manage outputs, then add a Decision element that checks {!ActionName.isSuccess} and handles the failure branch.