Sub topic 12
Fault Paths and Custom Errors
This chapter assumes you can build a record-triggered flow with entry conditions. It also assumes you know that a flow shares a database transaction with the save that triggered it, and that you have used Create Records and Get Records elements. It covers two tools for handling failure. A fault path decides what happens when an element fails. A custom error deliberately stops a save and tells the user why.
What happens when nothing handles a failure
Suppose an after-save flow tries to create a Task and the insert fails, for example because a required field is empty or a validation rule rejects it. If the failing element has no fault path, the flow stops with an unhandled fault.
For a record-triggered flow, an unhandled fault has three effects:
- The whole transaction rolls back, including the record the user was saving.
- The user sees an error message, often a long technical one.
- Salesforce sends a flow error email to the person who last modified the flow. Where the email goes can also be set in Process Automation Settings.
This is safe for your data but poor for users. A fault path lets you choose a different outcome.
Fault paths
A fault path is a connector that leaves an element and runs only if that element fails. In Flow Builder you add one by selecting the element's connector and choosing Fault. The fault connector is drawn in red.
Fault connectors are available on elements that can fail at run time, such as:
- Create Records, Update Records, and Delete Records
- Get Records
- Action elements, including Apex actions and email alerts
Assignment and Decision elements have no fault connector, because they do not call the database or external code. A before-save flow cannot contain Create, Update, or Delete Records elements, so it has few places where a fault path applies. In before-save flows, your main failure tool is the custom error described later in this chapter.
Reading the error with $Flow.FaultMessage
Inside a fault path, the global variable {!$Flow.FaultMessage} holds the text of the error. Two related variables are useful for logging: {!$Flow.InterviewGuid} identifies the running flow interview, and {!$Flow.CurrentDateTime} gives the time.
$Flow.FaultMessage only has a value after a fault has occurred. Reading it on the normal path gives you nothing useful.
A logging pattern
A common design routes every fault connector to one shared Create Records element that writes to a custom log object. Several fault connectors can point to the same element.
Flow: Opportunity - Create Follow-up Task (after-save)
Object: Opportunity
Trigger: A record is created or updated
Optimize for: Actions and Related Records
Entry condition: {!$Record.StageName} = "Closed Won"
Create Records element: Create_Task
Subject: Send welcome package
WhatId: {!$Record.Id}
Fault connector -> Log_Error
Create Records element: Log_Error (object: Flow_Error_Log__c)
Message__c: {!$Flow.FaultMessage}
Record_Id__c: {!$Record.Id}
Flow_Name__c: Opportunity - Create Follow-up Task
Interview_Guid__c: {!$Flow.InterviewGuid}Flow_Error_Log__c is a custom object you create yourself. It is not part of Salesforce by default.
Fault paths and the transaction
Adding a fault path changes the failure from "the flow failed" to "the flow handled the failure." This matters for the transaction.
- With no fault path, the flow fails and the whole transaction rolls back.
- With a fault path, the failed element's own changes are undone, but the flow continues on the fault branch. Unless something else fails, the transaction can still commit, so the user's save succeeds.
This has a practical consequence. If a fault path logs the error and ends, the user's save goes through even though your automation did not finish. That is usually right for a nice-to-have action such as a follow-up Task. It is wrong when the automation must succeed for the data to be valid.
Two related points:
- The log record you create in a fault path is part of the same transaction. If you then raise a custom error or something else rolls the transaction back, the log record is rolled back too. To keep a log through a rollback, publish a platform event instead, or log from an asynchronous path.
- The fault path is itself an element that can fail. Keep it small, with no extra lookups or complex logic. A fault path that fails ends in an unhandled fault.
Custom errors
A Custom Error element stops the save on purpose and shows a message you write. It works like a validation rule, but it can use anything a flow can use: related records, loops, collections, and formulas across several objects.
When the flow reaches a Custom Error element:
- The transaction rolls back, so the record is not saved.
- The user sees your message instead of a technical one.
- No flow error email is sent, because this is an intended outcome and not a failure.
You can show the message at the top of the record page, or attach it to a specific field so the field is highlighted. You can add more than one error to the element, each with its own location.
Where to put a custom error
Custom errors work in both before-save and after-save record-triggered flows, but they cost different amounts.
| Situation | Better choice | Reason |
|---|---|---|
| The rule uses only the triggering record's fields | Before-save with a Custom Error | No related-record work has run yet, so rejecting early is cheap. |
| The rule needs related records created or changed in the same flow | After-save with a Custom Error | The related records exist only after the save, and the rollback removes them. |
| The rule can be written as one formula on one object | A validation rule | Easier for admins to find and maintain. |
Example: require a Next Step on large closed deals
Flow: Opportunity - Require Next Step (before-save)
Object: Opportunity
Trigger: A record is created or updated
Optimize for: Fast Field Updates
Entry condition (Formula Evaluates to True):
AND(
ISCHANGED({!$Record.StageName}),
{!$Record.StageName} = "Closed Won",
{!$Record.Amount} > 100000,
ISBLANK({!$Record.NextStep})
)
Custom Error element
Message: A Closed Won deal over 100,000 needs a Next Step.
Show error on field: NextStepBecause the entry condition already contains the whole rule, the flow needs no Decision element. Any record that enters the flow is one that should be rejected.
Testing a custom error in Apex
A custom error reaches Apex as a DmlException. This test assumes the flow above is active.
@IsTest
private class OpportunityNextStepFlowTest {
@IsTest
static void largeClosedWonWithoutNextStepIsRejected() {
Opportunity opp = new Opportunity(
Name = 'Large deal',
StageName = 'Prospecting',
CloseDate = Date.today().addDays(30),
Amount = 200000
);
insert opp;
opp.StageName = 'Closed Won';
try {
update opp;
Assert.fail('Expected the flow to block this update.');
} catch (DmlException e) {
Assert.isTrue(
e.getMessage().contains('needs a Next Step'),
'Unexpected message: ' + e.getMessage()
);
}
}
}The test inserts the record in Prospecting, so the flow does not run on insert. It then moves the stage, which satisfies ISCHANGED and triggers the error. Assert.fail makes the test fail if the update succeeds.
Fault handling in other kinds of paths
Asynchronous and scheduled paths run in a separate transaction after the original save has committed. A failure there cannot roll back the original record. The user is no longer waiting, so nobody sees a message on screen. For these paths, a fault path that writes to a log object is often the only way anyone learns of the failure, so always add one.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| No fault path on a Create or Update element | Unhandled fault, full rollback, and a technical message for the user | Add a fault path, or deliberately accept the rollback |
| Fault path logs the error and nothing else, for required work | Users save records that miss required automation | Add a Custom Error or Roll Back Records element |
| Logging in the fault path, then raising a custom error | The log record is rolled back with everything else | Log with a platform event, or accept that no log is kept |
| Reading `$Flow.FaultMessage` outside a fault path | Empty value | Read it only in elements reached from a fault connector |
| Vague custom error text | Users cannot fix the problem themselves | Name the field and the rule in the message |
| Custom error on a rule a validation rule could handle | Logic hidden in a flow, harder for others to find | Use a validation rule for single-object formula checks |
Exercise
An after-save flow on Opportunity creates a Task when the stage becomes Closed Won. The Create Records element has a fault path that writes the value of $Flow.FaultMessage to a log object and then ends. The Task insert fails. Is the Opportunity saved, and why?
Show answer
Yes, the Opportunity is saved. The fault path handles the failure, the Task insert is undone, and the flow ends normally. With nothing else failing, the transaction commits, and the log record is committed with it. If the Task were required, you would add a Custom Error or Roll Back Records element after the log step to stop the save.
Exercise
You need to block a save when a related Account is flagged "On Hold", and you want the message shown on the Opportunity's Stage field. Which flow type and element fit best, and what must you check in the entry condition so the flow does not fire on every update?
Show answer
Use a before-save flow, because it only reads data and rejects early. Use a Get Records element for the Account, a Decision to check the flag, and a Custom Error element set to show the message on the StageName field. In the entry condition, use ISCHANGED({!$Record.StageName}) so the flow runs only when the stage changes.