Sub topic 1
When Flow needs Apex
Prerequisites
This chapter assumes you know basic Apex syntax (classes, methods, collections, SOQL) and the basics of Flow Builder (record-triggered and screen flows, variables, elements). You should also know what governor limits and DML are. You don't need to have written an invocable method yet. That starts in the next chapter.
Flow first, Apex second
Flow can do a lot without code. It can create, update, and delete records, run decisions and loops, send emails, and call subflows. For most automation, build it in Flow. Declarative automation is easier for admins to read, change, and debug.
Some work doesn't fit those elements. An invocable method is an Apex method that Flow can call as an action. The flow passes in values, your Apex runs, and the flow gets values back. Use one when the flow elements fall short.
Signs you need Apex
Reach for Apex when you see one of these situations:
- Complex logic. Heavy string parsing, date math, or calculations that take dozens of formula and assignment elements.
- Operations Flow lacks. Examples include sending a custom notification through an API, creating a record with a dynamic field list, or working with
Schemadescribe information. - Callouts you need to control. Calling an external service with custom headers, signing, retries, or response parsing.
- Performance. A loop in a flow that touches many records can be slow and hard to read. Apex can do the same job with collections and a single query.
- Reuse. One Apex method can serve many flows, and the same logic stays in one place.
- Unit testing. Apex has a testing framework with assertions. You can test logic directly instead of testing through a whole flow.
If a flow element does the job, don't write Apex for it. Each invocable method is code to maintain, test, and deploy.
How the call works
The flow doesn't call your class by name in a free-form way. You mark one method with an annotation. Salesforce then lists it in Flow Builder as an Apex action. The diagram shows the path of one call.
Two points matter from the start:
- The method receives a list, not a single value. Flow can batch several interviews (running instances of a flow) into one call.
- The method returns a list in the same order as the input, or returns nothing.
You will learn the details of both in later chapters.
A first look
This is the smallest useful shape. It takes text and returns a URL-friendly slug. Doing this in Flow alone would be awkward, because Flow has no regular-expression replace.
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 List<String> and returns List<String>. Each output matches the input at the same position. The label is the name an admin sees in Flow Builder.
Trade-offs to weigh
| Question | Flow elements | Invocable Apex |
|---|---|---|
| Who can change it? | Admins | Developers, through deployment |
| Testing | Flow tests, manual runs | Apex tests with assertions |
| Complex logic | Gets hard to read | Handled in code |
| Callouts and custom APIs | Limited | Full control |
| Maintenance cost | Lower | Higher |
Exercises
Exercise
A record-triggered flow needs to update a field with a value from a simple formula and send an email alert. Should you write an invocable method? Why or why not?
Show answer
No. A formula resource, an Update Records element, and an email action cover this. Apex adds code to test and maintain without any benefit.
Exercise
Name two reasons to prefer an invocable method over a loop of flow elements, and state what shape of input the method receives.
Show answer
Any two of: complex logic that is hard to read in Flow, performance with many records, reusable logic across flows, direct unit testing, or controlled callouts. The method receives a list of inputs, one per flow interview, and returns a list in the same order.