Sub topic 14
Handling errors with fault paths
This chapter assumes you can build a basic autolaunched or schedule-triggered flow. You should know Get, Create, Update, and Delete Records elements, and you should know what input and output variables are. No Apex is needed.
What a fault path is
A fault path is a connector that leaves an element and runs only when that element fails. Without one, an error stops the whole flow interview. An interview is one running instance of a flow.
For autolaunched and scheduled flows, nobody is watching a screen. An unhandled error therefore tends to show up late, as an error email or as records that were never processed. Salesforce sends that email to the person who last modified the flow, or to the Apex exception email recipients if you configured them. A fault path lets you log the failure, clean up, and tell the caller what happened.
Where you can add a fault path
Only elements that can fail at run time offer a fault connector.
| Element | Fault connector |
|---|---|
| Get, Create, Update, Delete Records | Yes |
| Action (Apex, email, invocable actions) | Yes |
| Subflow | No. Handle faults inside the child flow and return the status through output variables |
| Decision, Assignment, Loop | No |
In Flow Builder, select the element. Then drag a second connector from it and choose Fault when asked. The fault connector appears as a red dashed line.
Reading the error: $Flow.FaultMessage
Inside a fault path, the global variable {!$Flow.FaultMessage} holds the error text. It has a value only in a fault path. Outside one it is empty, so copy it into a regular variable or a record field right away.
Common messages include REQUIRED_FIELD_MISSING, FIELD_CUSTOM_VALIDATION_EXCEPTION, UNABLE_TO_LOCK_ROW, and INSUFFICIENT_ACCESS_OR_READONLY. These come from validation rules, required fields, record locks, and permissions.
What happens to data already changed
Handling a fault does not undo earlier work. When a fault path takes over, the flow keeps going, and DML that already ran in the same transaction stays in the pending state and will commit when the transaction ends. DML is a database write such as create or update.
If the earlier changes only make sense as a group, add a Roll Back Records element at the start of the fault path. It discards the pending changes in the current transaction. Then log the failure.
Order matters here. If you create the log record before the rollback, the rollback removes the log too. Put the rollback first and the log create after it.
A fault path in metadata
The snippet below is the part of a flow's XML for an Update Records element that points to an error logging element. It assumes a custom object Flow_Error_Log__c with the text fields Flow_Name__c and Error_Message__c.
<recordUpdates>
<name>Close_Case</name>
<label>Close Case</label>
<locationX>176</locationX>
<locationY>287</locationY>
<connector>
<targetReference>Next_Case</targetReference>
</connector>
<faultConnector>
<targetReference>Log_Error</targetReference>
</faultConnector>
<inputAssignments>
<field>Status</field>
<value>
<stringValue>Closed</stringValue>
</value>
</inputAssignments>
<inputReference>currentCase</inputReference>
</recordUpdates>
<recordCreates>
<name>Log_Error</name>
<label>Log Error</label>
<locationX>440</locationX>
<locationY>287</locationY>
<inputAssignments>
<field>Flow_Name__c</field>
<value>
<stringValue>Close_Stale_Cases</stringValue>
</value>
</inputAssignments>
<inputAssignments>
<field>Error_Message__c</field>
<value>
<elementReference>$Flow.FaultMessage</elementReference>
</value>
</inputAssignments>
<object>Flow_Error_Log__c</object>
</recordCreates>The faultConnector element is the only addition compared with a normal Update Records element. Everything else is ordinary flow metadata.
Pattern for autolaunched flows: report status to the caller
An autolaunched flow is usually called by something else. It should not fail silently, and it should not always crash the caller either. A reliable approach is to define two output variables:
isSuccess(Boolean), set totrueon the normal path andfalsein the fault path.errorMessage(Text), set from{!$Flow.FaultMessage}in the fault path.
The caller then decides what to do:
- A parent flow checks
isSuccessin a Decision element after its Subflow element. - Apex reads the values with
interview.getVariableValue('isSuccess'). - A REST client receives them in the
outputValuesof the response.
Set isSuccess to true at the start and change it to false in the fault path, or set it at the end of the normal path. Either way, make sure every path to an End sets it. A Boolean that is never assigned is false in a flow, so a missing assignment reports failure even for a successful run.
Pattern for scheduled flows: log and keep going
A scheduled flow processes many records, and one bad record should not hide the others. A fault path on the element that writes data lets you:
- Record the failing record's Id and the error message in a log object.
- Continue to the end of the flow.
- Let a report or list view on the log object show the failures.
If you update records in a loop, collect them into a collection variable and update once after the loop. That keeps DML out of the loop, as the previous chapter described. It also gives you one fault path to maintain instead of many. A failure in a bulk update tells you the batch failed. It does not always tell you which record caused it, so log the message and include the Id of the first record in the collection if that helps you investigate.
What fault paths cannot catch
Governor limit errors are not catchable. Examples are "Too many SOQL queries: 101" and "Too many DML statements: 151". When a limit is hit, the transaction ends and your fault path does not run. The way to handle limits is to design for them, so use bulk-safe patterns and avoid queries and DML inside loops.
Common mistakes
- Reading
$Flow.FaultMessageoutside a fault path. It is empty there. - Creating the log record before Roll Back Records, so the log is rolled back too.
- Assuming a handled fault undoes earlier changes. It does not without a rollback.
- Putting a fault path only on the last element and leaving earlier Get and Create elements unhandled.
- Expecting a fault path to catch a governor limit exception.
- Sending a fault email on every failure from a scheduled flow that processes thousands of records, which floods inboxes. Log to an object instead.
Exercise
A scheduled flow updates a collection of Cases. The Update Records element has a fault path with a Create Records element (error log) followed by a Roll Back Records element. The log record never appears after a failure. What is wrong and how do you fix it?
Show answer
The rollback runs after the log create, so it discards the log record along with the other pending changes. Swap the order: Roll Back Records first, then Create Records for the log, so the log is committed with the transaction.
Exercise
An autolaunched flow is called by Apex and its Create Records element can fail. What would you add so the Apex caller can tell whether the run succeeded, and what value would the fault path assign?
Show answer
Add output variables isSuccess (Boolean) and errorMessage (Text). On the fault path, assign isSuccess = false and errorMessage = {!$Flow.FaultMessage}. Apex then reads both with interview.getVariableValue after start().