Sub topic 14
Handling errors with fault paths
This chapter assumes you can build a basic autolaunched or schedule-triggered flow and know the Get, Create, Update, and Delete Records elements. It covers what happens when one of those elements fails and how to respond.
What a fault path is
A fault path is a connector that a flow follows when a data or action element fails. Without one, an unhandled error stops the whole interview. For a scheduled flow, the failed record's changes roll back and Salesforce sends an error email to the user who last modified the flow.
You add a fault path from an element in Flow Builder. Select the element, then choose Add Fault Path from the connector menu. The fault connector appears in red and leads to the elements you want to run when the failure occurs.
Elements that support fault paths include Create, Update, Delete, and Get Records, as well as Action and Subflow elements.
Reading the error
When a fault path runs, the flow stores the error text in the global variable $Flow.FaultMessage. Use it in a text template, a Create Records element, or an output variable to record what went wrong.
A common pattern for a scheduled flow that updates many records:
- Update Records changes the selected records.
- The fault path goes to an Assignment that builds a message from
$Flow.FaultMessage. - A Create Records element writes a custom log record, such as
Flow_Error_Log__c, with the flow name, the record ID, and the message. - Optionally, an Action element sends an email to an admin.
Faults in autolaunched flows
An autolaunched flow runs for a caller, so decide who reports the error. Two common choices:
- Handle it inside the flow: log the failure and finish normally. The caller sees a successful run, so it cannot react.
- Pass it back: set an output variable such as
errorMessage(Text) from$Flow.FaultMessage, then end the flow. The caller checks that variable in a Decision element or in Apex withgetVariableValue.
The second choice is easier to test and lets a parent flow decide what to do.
Common mistakes
- No fault path on DML elements. One bad record can end the run and leave no useful trace.
- Logging inside a loop. A Create Records element inside a Loop counts against DML limits. Add log records to a collection variable, then create them once after the loop.
- Fault path loops back to the failing element. This repeats the same error until the limit is hit. Route the fault path forward instead.
- Hiding the error. If the fault path ends silently, nobody learns about the failure. Always log, notify, or return the message.
Exercise
A scheduled flow updates records inside a Loop and has a fault path that creates one error log record per failure. Why can this cause a new problem, and how do you fix it?
Show answer
Creating a record inside the loop adds one DML statement per failure, which can exceed the DML limit. Instead, add each log record to a collection variable in the fault path, and use one Create Records element after the loop to insert the whole collection.