Sub topic 8
Reporting errors and using fault paths
This chapter assumes you can write a bulk-safe @InvocableMethod with request and result wrapper classes. You should also know how to add an action to a flow in Flow Builder. Here you will learn two ways an invocable method can report a failure and when to use each.
Two kinds of failure
An invocable method can fail in two ways.
- The whole call fails. The method throws an exception and returns nothing.
- One input fails. The method returns normally, and one entry in the result list says that input did not work.
The first kind is handled by a fault path in the flow. The second kind is handled by the flow checking a value in the result.
Throwing exceptions
Throw an exception when the method cannot do its job at all. An unhandled exception, whether yours or a platform one such as a DML error, is reported to the flow. If the action has a fault connector, the flow follows it.
public with sharing class SafeAccountUpdater {
public class UpdateException extends Exception {}
@InvocableMethod(label='Rename Accounts')
public static void renameAccounts(List<Id> accountIds) {
List<Account> accounts = [SELECT Id, Name FROM Account WHERE Id IN :accountIds];
if (accounts.isEmpty()) {
throw new UpdateException('No matching accounts were found.');
}
for (Account acc : accounts) {
acc.Name = acc.Name.trim();
}
update accounts;
}
}Write the exception message for the person who will read it. The flow can show it to a user or log it.
Returning per-record results
For partial failures, return a result for every input, as in AccountTaskCreator from the earlier chapter. Two details make this work.
- Call
Database.insert(tasks, false). The second argument isallOrNone. When it isfalse, valid records are saved even if others fail, and you get oneSaveResultper record. - Build the result list in the same order as the input list. Flow matches results to interviews by position.
Each result carries isSuccess and errorMessage. Read the message with saveResults[i].getErrors()[0].getMessage(), and only when isSuccess() is false. Calling it on a successful result throws an exception, because the error list is empty.
Building the fault path in Flow Builder
A fault connector is not added automatically. To add one:
- Select the Apex action element on the canvas.
- Drag a connector from the element and choose Fault when asked.
- Connect it to the elements that should run when the action fails.
On the fault path, the global variable {!$Flow.FaultMessage} holds the error text. Common uses are a Screen element that shows the message, a Create Records element that writes an error log record, or an Assignment that sets a status field.
Without a fault connector, the flow stops with an unhandled error. In a record-triggered flow, the user who saved the record usually sees the error and the save fails. The admin also receives a flow error email.
Choosing between them
| Situation | Approach | Flow handles it with |
|---|---|---|
| Required setup is missing, or nothing can proceed | Throw an exception | Fault connector |
| One record fails validation, others are fine | Return `isSuccess = false` and a message | Decision on the result |
| Unexpected platform error such as a DML failure | Let it propagate, or catch and rethrow with a clearer message | Fault connector |
Common mistakes
- Swallowing exceptions. A
catchblock that does nothing makes the flow look successful while the work was not done. Either rethrow or record the failure in the result. - Returning a shorter list. If you skip failed inputs, results shift position and map to the wrong interviews. Always add one result per request.
- Throwing for a single bad record. This fails every interview in the batch. Return a failed result instead.
- Reading
getErrors()[0]unconditionally. CheckisSuccess()first.
Exercise
A flow calls your invocable method with 50 interviews batched together. Input 12 has an invalid value and the other 49 are fine. Should the method throw an exception or return a result with isSuccess = false for input 12, and why?
Show answer
It should return a result with isSuccess = false (and an error message) for input 12, and normal results for the others, all in input order. A thrown exception applies to the whole call, so it would fail all 50 interviews. The flow can then check isSuccess in a Decision element and handle the one failure.