Sub topic 5
Handling errors with fault paths
This chapter assumes you can build a basic flow in Flow Builder, you know what variables and record-triggered flows are, and you have a sandbox or Developer Edition org to try things in. A fault path catches an error at one element and sends the flow down a different route, so you can log or report the problem instead of letting the whole interview fail.
What a fault path is
When an element such as Create Records, Update Records, Get Records, Delete Records, or an action fails, the flow stops by default. The user sees a generic error, and any changes made in the same transaction are rolled back. A fault path is an extra connector that runs only when that element fails.
To add one in Flow Builder:
- Select the element that can fail.
- Click the element's lower connector point and choose Fault.
- Connect the fault connector to the elements that should handle the error.
The fault connector is drawn in red. Each element can have one fault connector.
Reading the error with $Flow.FaultMessage
On a fault path, the global variable $Flow.FaultMessage holds the text of the error. Use it in a Text Template, an email body, or a field on a log record. This template works in a record-triggered flow:
An error occurred in the flow: {!$Flow.FaultMessage}
Flow: {!$Flow.CurrentDateTime}
Record Id: {!$Record.Id}$Record.Id exists only in record-triggered flows. In a screen flow or an autolaunched flow, pass the record Id in through a variable and reference that variable instead.
What happens to the transaction
A fault path does not undo the failure, and it changes what the user sees. Consider these points:
- If the fault path ends normally, the flow does not show an unhandled error. A screen flow can display a friendly screen instead.
- In a record-triggered flow, the work that failed is not saved, but the flow itself finishes. The save of the triggering record can still go ahead.
- Records created on the fault path, such as an error log, are part of the same transaction. If something later in the transaction throws an unhandled error, they are rolled back with everything else.
- If you want the user to see the failure and stop the save, end the fault path with a Custom Error element where the flow type supports it. This rolls back the transaction and shows your message.
Decide for each flow whether a failure should be silent, logged, or blocking. Do not leave this to chance.
Choosing a handling pattern
| Pattern | Use it when | Trade-off |
|---|---|---|
| Show a screen with the message | Screen flows where the user can retry | Only helps interactive users |
| Create an error log record | You want a searchable history | Needs a custom object and the record is lost if the transaction rolls back |
| Send an email to an admin | Failures need quick attention | Can flood inboxes in bulk updates |
| Custom Error element | The save must be blocked | The user sees your message and nothing is saved |
A worked example
Suppose a record-triggered flow on Opportunity creates a follow-up Task when the stage becomes Closed Won. The Create Records element can fail, for example when a validation rule or a required field rejects the Task.
- Open the flow and select the Create Records element for the Task.
- Add a fault connector from it.
- Add a Create Records element that writes an
Error_Log__crecord. This is a custom object you create with fields such asMessage__c(Long Text Area) andRelated_Id__c(Text). - Set
Message__cto{!$Flow.FaultMessage}andRelated_Id__cto{!$Record.Id}. - Save the flow and run it with the debugger, using an input that makes the Task fail.
In the debugger, the path highlights through the fault connector, and the error log element shows the stored message. If you do not see the fault path highlighted, the failing element has no fault connector, or the failure came from a different element.
Common mistakes
- Putting a fault path only on some elements. An unprotected element still fails the whole interview. Add one to every element that touches the database or calls an action.
- Letting the fault path fail too. If the log element has its own error, such as a required field left empty, the interview fails anyway. Keep the handling simple.
- Creating error records inside a loop. A fault path inside a loop that writes one record per failure can reach DML limits. Collect errors in a collection variable and write them once after the loop.
- Hiding errors. Swallowing a failure with no log or message makes problems harder to find later.
Exercise
A record-triggered flow creates a Task, and the fault path writes an ErrorLogc record. Later in the same transaction, an unrelated Apex trigger throws an unhandled exception. Will the ErrorLog__c record exist afterward, and why?
Show answer
No. The error log record was created in the same transaction, and an unhandled exception rolls back every change in that transaction, including records created on the fault path. To keep the log, write it in a separate transaction, for example with a platform event or an asynchronous path.