Sub topic 3
Signature and class rules you must follow
Flow Builder finds invocable methods by inspecting your compiled Apex. If a method breaks one of the rules below, either the class won't save or the action won't appear in Flow Builder. This chapter lists the rules, explains why each one exists, and shows the compile-time and runtime failures you are most likely to hit.
This chapter assumes you can write Apex classes with static methods and collections. It also assumes you know what a flow interview is (one running instance of a flow) and have seen the Action element in Flow Builder. Bulkification, wrapper class design, and fault paths each get their own chapter. They appear here only where they affect the signature.
The rules at a glance
An invocable method must meet all of these conditions:
- The class contains exactly one method annotated with
@InvocableMethod. - The method is
static. - The method is
publicorglobal. - The class that contains it is an outer (top-level) class, not an inner class.
- The method takes at most one parameter, and that parameter is a
List. - The method returns
voidor aList. - The element types in the parameter and return lists come from a fixed set of supported types.
- No other annotation is combined with
@InvocableMethodon the same method.
The sections below take these in turn.
One method per class
Apex allows only one @InvocableMethod in a class. This is the rule people notice first, because it forces a design choice. If you have three related actions, such as "create task", "close task", and "reassign task", you need three classes.
public with sharing class CloseTasksAction {
@InvocableMethod(label='Close Tasks' category='Task')
public static void closeTasks(List<Id> taskIds) {
List<Task> tasks = [SELECT Id FROM Task WHERE Id IN :taskIds];
for (Task t : tasks) {
t.Status = 'Completed';
}
update tasks;
}
}Keep the shared logic in a separate service class that each action class calls. The action classes then stay thin adapters between Flow and your business logic.
Static, and public or global
Flow calls the method without creating an instance of your class, so the method must be static. It must also be visible to the Flow runtime, so it must be public or global.
Use public for code in your own org. Use global only in a managed package, where the action must remain callable by subscribers' flows. A global method also requires a global class.
// Does not compile: the method is not static.
public class BadAction1 {
@InvocableMethod
public List<String> run(List<String> inputs) {
return inputs;
}
}
// Does not compile as an invocable method: private access.
public class BadAction2 {
@InvocableMethod
private static List<String> run(List<String> inputs) {
return inputs;
}
}The class must be an outer class
The annotated method must live in a top-level class. Putting it in an inner class fails, even if the inner class is public static.
Inner classes are still useful, but for something else. The request and response wrapper classes can be inner classes of the action class, as the AccountTaskCreator example in this tutorial does. Only the method itself must be in the outer class.
public with sharing class OrderActions {
// Fine: a wrapper type nested inside the outer class.
public class Request {
@InvocableVariable(required=true)
public Id orderId;
}
// Fine: the invocable method sits directly in the outer class.
@InvocableMethod(label='Approve Orders')
public static void approve(List<Request> requests) {
// ...
}
}At most one parameter, and it is a list
Flow can call your method for many interviews in the same transaction. The platform therefore passes one list containing one element per interview. A single scalar parameter couldn't represent that, so the platform requires a list.
Two consequences follow:
- You can't declare
String name, Integer count. If the flow needs to send several values, define a wrapper class with one@InvocableVariableper value and takeList<ThatWrapper>. - You can omit the parameter entirely. A method with no parameters is legal, but it can't receive any data from the flow.
// Does not compile: two parameters.
public class BadAction3 {
@InvocableMethod
public static void run(String name, Integer count) {}
}
// Does not compile: not a list.
public class BadAction4 {
@InvocableMethod
public static void run(String name) {}
}Return type: void or a list
The return type is void or a List of a supported type. Choose by asking whether the flow needs anything back.
- Use
voidwhen the flow only needs the side effect, as in theSafeAccountUpdaterexample from the outline. - Use a list when the flow reads outputs, such as new record Ids or status values.
When you return a list, it should have one element for each input element, in the same order. Flow matches outputs to interviews by position, so a shorter or reordered list silently assigns results to the wrong interview. The platform doesn't verify this for you. The bulkification chapter covers it in detail.
Supported element types
The same type set applies to the parameter list and the return list.
| Element type | Example | Notes |
|---|---|---|
| Primitive | `List<String>`, `List<Id>`, `List<Integer>`, `List<Boolean>`, `List<Date>` | The simplest shape. Good for one-value inputs. |
| List of primitives | `List<List<String>>` | Lets each interview pass or receive a collection. |
| Concrete sObject | `List<Account>`, `List<Invoice__c>` | The flow supplies record variables. |
| Generic sObject | `List<SObject>` | Accepts any record type. You check the type at runtime. |
| Apex-defined class | `List<MyRequest>` | Fields must carry `@InvocableVariable`. |
Not supported as the parameter or return element type are Map, Set, and other collection types, including a bare Set<Id>. If you need a map-like structure, put it in a wrapper class, for example as a list of key-value wrapper objects.
// Does not compile as an invocable method: Set is unsupported.
public class BadAction5 {
@InvocableMethod
public static void run(Set<Id> ids) {}
}Rules for Apex-defined types
When you use your own class as the element type, the platform inspects it and exposes its annotated fields to Flow Builder. Several details matter:
- The class must be
publicorglobal. Aprivatewrapper class can't be seen by Flow. - Only fields marked
@InvocableVariableare visible. Unannotated fields are ignored. - Annotated fields must be
publicorglobal, and must be instance variables. Static fields are not exposed. - Annotated fields must use supported types: primitives, sObjects, or other Apex-defined types, and lists of these.
- The class needs a usable no-argument constructor. The platform creates instances itself, so if you declare only a constructor with parameters, the default one disappears and instantiation fails.
public with sharing class InvoiceActions {
public class Request {
@InvocableVariable(required=true)
public Id invoiceId;
@InvocableVariable
public Decimal discountPercent;
// Not exposed to Flow: no annotation.
public String internalNote;
}
public class Result {
@InvocableVariable
public Boolean isSuccess;
@InvocableVariable
public String message;
}
@InvocableMethod(label='Apply Discount')
public static List<Result> applyDiscount(List<Request> requests) {
List<Result> results = new List<Result>();
for (Request req : requests) {
Result res = new Result();
res.isSuccess = req.discountPercent != null && req.discountPercent >= 0;
res.message = res.isSuccess ? 'Accepted' : 'Discount must be zero or more.';
results.add(res);
}
return results;
}
}The Request class has no constructor, so Apex supplies a default one. If you later add public Request(Id invoiceId) {...}, also add public Request() {}.
Annotation parameters
@InvocableMethod takes optional parameters that affect only how the action appears in Flow Builder. They don't change how the method runs.
| Parameter | Purpose |
|---|---|
| `label` | The name shown in the action list. Defaults to the method name. |
| `description` | Help text shown in Flow Builder. |
| `category` | Groups the action under a heading in the Action picker. |
| `callout` | Set to `true` if the method makes callouts to external systems. |
| `iconName` | Icon shown beside the action. |
| `configurationEditor` | A custom property editor component for the action. |
Always set label and description. Actions are chosen by admins who will not read your Apex, and the method name createTasks tells them less than "Create Follow-up Tasks".
No stacking with other annotations
You can't combine @InvocableMethod with another method-level annotation such as @AuraEnabled or @future. If you need the same logic from a Lightning component, put it in a service class and give each entry point its own class.
Checking a signature quickly
The decision path below reflects the checks the platform applies. If any answer is "no", the method isn't a valid invocable method.
Common mistakes
- Putting two annotated methods in one class "to keep related code together".
- Declaring the method inside an inner class.
- Using
Set<Id>orMap<Id, String>as the parameter. UseList<Id>or a wrapper. - Forgetting
publicon the wrapper class or its fields, so Flow Builder shows no inputs. - Writing a constructor with arguments on a wrapper class without also writing a no-argument one.
- Returning a list with a different length from the input list.
Exercise
A teammate writes @InvocableMethod public static void notify(Set<Id> contactIds, String message) in a public inner class of NotificationService. List every rule it breaks, and show a corrected signature.
Show answer
It breaks four rules. (1) The method is in an inner class, but it must be in an outer class. (2) It has two parameters, but only one is allowed. (3) Set<Id> is not a supported type, and the parameter must be a List. (4) Because of (2), message has nowhere to go except a wrapper class. A corrected version is a top-level class NotifyContacts with public class Request { @InvocableVariable(required=true) public Id contactId; @InvocableVariable public String message; } and @InvocableMethod(label='Notify Contacts') public static void notify(List<Request> requests).
Exercise
Why does the platform require the parameter to be a List even when your flow only ever handles one record at a time?
Show answer
A record-triggered flow can run for many records in one transaction, and the platform batches the Action calls from those interviews into a single invocation of your method. The list carries one element per interview. The signature must always accept a list, whether or not your current flow uses it that way.