Sub topic 3
Reporting Errors and Using Fault Paths
Assume you know basic Apex, SOQL, DML, Flow Builder, and governor limits. This chapter focuses on what happens when an invocable action fails and how to keep the method safe in production.
Reporting Errors and Using Fault Paths
An unhandled Apex exception marks the Flow action as failed. In Flow Builder, connect the action’s Fault connector to an error-handling path. That path can create an error record, show a message in a screen flow, or send an alert.
Use exceptions for failures that should stop the action. For expected record-level failures, return one result for each request instead:
public with sharing class AccountTaskCreator {
public class Request {
@InvocableVariable(required=true)
public Id accountId;
}
public class Result {
@InvocableVariable
public Boolean success;
@InvocableVariable
public String errorMessage;
}
@InvocableMethod(label='Create Tasks')
public static List<Result> createTasks(List<Request> requests) {
List<Task> tasks = new List<Task>();
for (Request request : requests) {
tasks.add(new Task(
WhatId = request.accountId,
Subject = 'Follow up'
));
}
List<Database.SaveResult> saves = Database.insert(tasks, false);
List<Result> results = new List<Result>();
for (Database.SaveResult save : saves) {
Result result = new Result();
result.success = save.isSuccess();
if (!result.success) {
result.errorMessage = save.getErrors()[0].getMessage();
}
results.add(result);
}
return results;
}
}The false argument allows partial success. The output list keeps the same order as the input list, so Flow can match each result to its request.
Testing Invocable Methods
Call the method directly in an Apex test. Include a bulk test with many inputs, verify the result count, and check both successful and failed records. Test.startTest() and Test.stopTest() isolate governor-limit measurements.
Transaction Context and Governor Limits
An invocable method normally runs in the same transaction as the Flow. Its SOQL queries, DML statements, CPU time, and heap usage count against that transaction’s limits. Query and update records in collections, not inside loops.
Sharing and Data Access
Use with sharing unless the method explicitly requires system-level record access. Sharing does not enforce field-level security or object permissions automatically. For user-controlled data, consider WITH USER_MODE, user-mode DML, or Security.stripInaccessible.
Making Callouts from Invocable Methods
For an external HTTP request, declare @InvocableMethod(callout=true). Use a named credential instead of storing endpoint credentials in Apex. Design for timeouts and external failures, and avoid mixing a callout with pending uncommitted DML in the same transaction.
Design Practices and Known Limitations
Keep one invocable method focused on one business operation. Accept wrapper objects when the action needs multiple inputs, and expose only fields Flow must map. Document whether failures stop the whole action or appear in individual results. Invocable methods have a list-based contract and limited interface features, so complex orchestration may belong in Flow or another service layer.
Exercise
When should an invocable method return a failure result instead of throwing an exception?
Show answer
Return a failure result when one input can fail independently and the Flow should continue processing other inputs. Throw an exception when the entire action cannot continue.