Sub topic 3
Signature and Class Rules You Must Follow
This chapter assumes you know basic Apex syntax (classes, methods, collections, SOQL) and have used Flow Builder. It covers the rules the platform enforces when you mark a method with @InvocableMethod. Breaking them causes a compile error when you save the class, or the action fails to appear in Flow Builder.
The rules at a glance
An invocable method must meet all of these conditions:
- It is
static. - It is
publicorglobal. - Its class is
publicorglobalas well. The class can be an outer class or an inner class. - It takes exactly one parameter, and that parameter is a
List. - It returns
voidor aList. - It is the only
@InvocableMethodin its class.
The table shows what the compiler accepts for the parameter and return types.
| Part | Allowed | Not allowed |
|---|---|---|
| Parameter | `List<String>`, `List<Id>`, `List<Account>`, `List<MyRequest>` | `String`, `Set<Id>`, `Map<Id, String>`, two parameters |
| Return type | `void`, `List<String>`, `List<MyResult>` | `String`, `Boolean`, `Map<Id, String>` |
| Modifiers | `public static`, `global static` | non-static, `private`, `protected` |
A minimal valid method
This class follows every rule. It takes a list of strings and returns a list of strings.
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 is public static. It has one List<String> parameter. It returns a List<String> with one entry for each input, in the same order.
One method per class
Only one method in a class can carry @InvocableMethod. If you need two flow actions, create two classes. Inner classes do not help here. Each action still needs its own top-level class for the method.
Keep related logic in a shared helper class and give each action a thin wrapper:
public with sharing class AccountActions {
public static void normalizeNames(List<Account> accounts) {
for (Account acc : accounts) {
acc.Name = acc.Name.trim();
}
}
}public with sharing class NormalizeAccountNamesAction {
@InvocableMethod(label='Normalize Account Names')
public static void run(List<Account> accounts) {
AccountActions.normalizeNames(accounts);
update accounts;
}
}Allowed parameter element types
The list can hold several kinds of element:
- Primitives such as
String,Integer,Boolean,Decimal, andId. - sObjects, either a specific type like
Accountor the genericSObject. - A custom Apex class whose fields use
@InvocableVariable. The next chapter covers this annotation.
Do not use a list of lists. The platform does not support nested collections as the direct parameter type. If a flow needs to pass several values for one record, wrap them in a request class instead.
Why the signature is a list
Flow can run many interviews at once. A record-triggered flow that fires for 200 records in one transaction sends all 200 sets of inputs to your method in a single call. The list parameter is how the platform delivers them. Your code must loop over the whole list and not assume it has one item.
The returned list must match the input list. The result at index 3 belongs to the input at index 3. If you return fewer items, or a different order, flows receive wrong values. A later chapter covers this ordering in detail.
The rest of the class
A few other points apply to the surrounding class:
- Use
with sharingunless you have a reason not to. The method runs in the context the class declares. - Give the method a
labelso it is easy to find in Flow Builder. Without it, the action shows the method name. - Add a
descriptionwhen the action's purpose is not obvious. - A
categorygroups the action in the Flow Builder picker. - Do not mark the method
@future, and do not combine it with another method-level annotation that changes its behavior. Choose one role for the method.
Common compile errors
- Non-static method: the compiler requires
static. Add the keyword. - More than one parameter: combine the values into a request class with
@InvocableVariablefields. - Two
@InvocableMethodannotations in one class: move one into a new class. - Invalid return type: return
voidor aList. To return one value, return a list with one element per input.
Exercise
A developer writes @InvocableMethod public static String getName(Id accountId). List every rule this signature breaks and show a corrected signature that returns the account name for each input.
Show answer
It breaks two rules. The parameter is a single Id and not a List, and the return type is a single String and not a List or void. A corrected version is @InvocableMethod public static List<String> getNames(List<Id> accountIds). The body must return one name per input Id, in the same order.