Sub topic 13
Design practices and known limitations
This chapter assumes you can write an @InvocableMethod with request and response classes, that you know bulkification and fault paths, and that you have built a basic flow. It collects the design rules that keep invocable Apex maintainable, and the limits you need to plan around.
Decide whether Apex belongs in the flow
Start with the built-in elements. Reach for Apex only when a flow cannot do the job cleanly. Typical reasons are complex calculations, callouts that need custom handling, work across many records at once, and logic that needs unit tests.
Keep the invocable method thin
An invocable method is an entry point for Flow. Put business logic in a separate service class, and let the invocable class convert between flow data and that logic. The same logic can then be reused from triggers, batch jobs, and tests.
public with sharing class FollowUpService {
public static List<Task> buildTasks(List<Id> accountIds) {
List<Task> tasks = new List<Task>();
for (Id accountId : accountIds) {
tasks.add(new Task(
WhatId = accountId,
Subject = 'Follow up',
ActivityDate = Date.today().addDays(7)
));
}
return tasks;
}
}public with sharing class FollowUpAction {
@InvocableMethod(
label='Create Follow-up Tasks'
description='Creates one follow-up task for each account Id.'
category='Account'
)
public static List<Id> run(List<Id> accountIds) {
List<Task> tasks = FollowUpService.buildTasks(accountIds);
insert tasks;
List<Id> taskIds = new List<Id>();
for (Task t : tasks) {
taskIds.add(t.Id);
}
return taskIds;
}
}The output list has one entry per input, in the same order, so the flow can match results to interviews.
Practices that pay off
- One job per method. A method named for a single action is easier to reuse in flows than one with a mode parameter.
- Write clear labels and descriptions. Flow Builder shows them to admins, who may never read your code.
- Prefer wrapper classes for more than one input. A method accepts a single list parameter, so a request class is how you pass several values.
- Return per-record results when partial failure is possible, as in the
Resultclass from earlier chapters. - Declare sharing deliberately. Use
with sharingunless you have a documented reason not to. - Avoid hard-coded IDs and names. Pass them in from the flow or read them from custom metadata.
- Test with bulk input. Call the method with 200 requests, not one.
Known limitations
| Limitation | What it means for you |
|---|---|
| One `@InvocableMethod` per class | Each action needs its own class. Group shared logic in service classes. |
| Method must be `static` and `public` or `global` | Instance methods and private methods are not exposed. |
| One parameter, and it must be a list | Use a request class to carry several values. |
| Return type is `void` or a list | Use a response class to return several values. |
| Generic `SObject` is not supported as a parameter type | Use a specific type such as `Account`, or a wrapper class. |
| Flow-facing signatures are hard to change | Flows save references to your class and its variables. |
The last row causes the most trouble in practice. Renaming a class, changing a variable type, or removing an @InvocableVariable field can break flows that already use the action. Add new optional fields instead of changing existing ones. Check which flows use an action before you remove or rename anything.
Plan for platform limits
Flow and Apex share one transaction, so the action counts against the same governor limits as the rest of the flow. A flow that runs your method after several queries and DML elements has less headroom than your test suggests. Keep SOQL and DML outside loops, and measure with Limits in a realistic test.
Exercise
A teammate wants to add a second @InvocableMethod to AccountTaskCreator so one class can both create tasks and close them. Why will this fail, and what is the better design?
Show answer
A class can contain only one @InvocableMethod. Create a separate invocable class for each action, such as one for creating tasks and one for closing them, and have both call shared logic in a service class.