Sub topic 2
Marking a method with @InvocableMethod
This chapter assumes you can write basic Apex (classes, methods, collections, SOQL) and have used Flow Builder to build a record-triggered or screen flow. You should also know what governor limits and DML are. Here you will learn what the @InvocableMethod annotation does, which properties it accepts, and the mistakes that most often stop a method from showing up in a flow.
What the annotation does
@InvocableMethod marks a static Apex method so that Flow Builder can list it as an action. Without the annotation, a flow cannot call your code, even if the method is public and static.
The annotation is only a marker plus metadata. It does not change how the method runs. The method still executes in the same transaction as the flow that called it, and it still counts against the same governor limits.
A minimal example
The smallest useful method takes a list in and returns a list out. The output list has the same size and order as the input.
public class SlugGenerator {
@InvocableMethod(label='Make Slug' description='Lowercases text and replaces spaces with hyphens.')
public static List<String> makeSlugs(List<String> inputs) {
List<String> slugs = new List<String>();
for (String input : inputs) {
slugs.add(input == null ? null : input.trim().toLowerCase().replaceAll('\\s+', '-'));
}
return slugs;
}
}The method takes a List<String> because a flow may batch several interviews into one call. Each interview contributes one element. Return one slug per input, in the same position, so each interview gets its own answer.
Annotation properties
All properties are optional, but label and description make the action much easier to find and understand in Flow Builder.
| Property | Purpose |
|---|---|
| `label` | The name shown in the action list. Defaults to the method name. |
| `description` | Help text shown to the flow builder. |
| `category` | Groups the action under a heading in the action picker. |
| `callout` | Set to `true` if the method makes callouts. Defaults to `false`. |
| `configurationEditor` | Names a custom property editor component for the action. |
| `iconName` | Names an icon to show with the action. |
| `namespace` | Namespace to apply to the action, mainly for managed packages. |
Here is the annotation with several properties set:
@InvocableMethod(
label='Create Follow-up Tasks'
description='Creates one follow-up task per request and returns one result per request.'
category='Account'
)Property values must be constants. You cannot compute a label at runtime.
How a flow reaches the method
When a flow runs an Apex action, Salesforce collects the inputs from the interviews that reached that element. It makes one call to your method with a list. It then maps each returned item back to the interview that supplied the same position.
This is why the method must treat its parameter as a list and never assume it has one element. Bulk handling is covered in its own chapter, but the shape of the signature comes from this behavior.
Common errors
- Method is not static. Invocable methods must be static. Instance methods fail to save.
- Two annotated methods in one class. A class may contain only one
@InvocableMethod. Move the second method to another class. - Wrong parameter shape. The method takes at most one parameter, and it must be a list. A single
StringorIdis not allowed. UseList<String>orList<Id>. - Class not visible. The class and method must be
publicorglobal. Aprivatemethod will not appear in the flow. - Missing sharing keyword. The code still compiles, but it runs in the default sharing context of the caller's setup. Declare
with sharingorwithout sharingdeliberately. - Callout without the flag. If the method makes an HTTP callout, set
callout=trueon the annotation. Otherwise the flow may reject the action in contexts where callouts are not allowed.
Reading a failure
If the class does not save, read the compiler message. Messages such as "Invalid invocable method" usually point to the signature rules above. If the class saves but the action is missing from the flow, check that the method is public or global, and that you are searching under the correct category.
Exercise
A teammate adds @InvocableMethod to public static String getName(Id accountId). The class fails to save. Name two changes that fix it.
Show answer
Change the parameter to a list, such as List<Id> accountIds, and change the return type to a list, such as List<String>, or to void. The method must then return one value per input, in the same order, when it returns a list.
Exercise
Which annotation property would you set so the action appears under an "Account" heading in the action picker, and what must be true of the value you give it?
Show answer
Set category='Account'. The value must be a constant string, because annotation properties cannot be computed at runtime.