Sub topic 13
Testing and debugging record-triggered flows
A record-triggered flow runs inside the save transaction, so you can't test it by watching a screen. You test it by saving records and checking what changed. This chapter covers the tools for that: the Flow Builder debugger, Flow Builder tests, debug logs, Flow Trigger Explorer, and Apex tests.
This chapter assumes you can build a before-save and an after-save flow. It also assumes you know $Record and $Record__Prior, and that you know a flow shares the transaction and governor limits with Apex and other automation.
Choosing a tool
Each tool answers a different question, so pick by question.
| Tool | Best for | Saves data? |
|---|---|---|
| Flow Builder Debug | "Does my logic take the path I expect?" | Optional, rollback by default |
| Flow Builder Tests | Repeatable checks on one flow | No, rolled back |
| Debug logs | "What happened in a real save, with other automation?" | Real transaction |
| Flow Trigger Explorer | "Which flows run on this object, and in what order?" | Not applicable |
| Apex tests | Regression checks across flows, triggers, and validation rules | No, rolled back |
Debugging in Flow Builder
Open the flow and select Debug. For a record-triggered flow you choose an existing record to act as the triggering record. For an update flow, you also enter the new field values. Builder treats the record's stored values as $Record__Prior and your edits as $Record.
This matters for entry conditions that use ISCHANGED. The condition from the spine only passes when the stage actually changes:
AND(
ISCHANGED({!$Record.StageName}),
{!$Record.StageName} = "Closed Won"
)If you debug with the stage left unchanged, the flow won't start. That is correct behavior, not a bug. Choose a record whose stage is not Closed Won, then set the stage to Closed Won in the debug inputs.
Useful debug options:
- Run flow in rollback mode. This is on by default. The flow runs for real, then the transaction is rolled back. Turn it off only in a sandbox, and only when you want the changes kept.
- Run as another user. Use this to reproduce a problem a specific profile or permission set reports. It matters when the flow runs in user context.
- Show details. This lists every element visited with its input and output values.
- Scheduled path selection. If the flow has a scheduled path, you can choose which path to debug. It runs immediately instead of waiting for the scheduled time.
In the debug output, read in order. First look for the start element and whether the entry conditions passed. Then check each Decision outcome, the values of your variables, and the records created or updated.
Before-save flows have no DML for their own changes, so the debugger shows Assignment results but no record creation. After-save flows show the Create Records or Update Records results, including the new record IDs.
Debugger limits
The debugger runs the flow in isolation, as the record being tested and the user who started the debug. It does not run your Apex triggers, validation rules, or duplicate rules as part of the debug. A flow that passes in the debugger can still fail in a real save. Use debug logs or Apex tests to see the whole transaction.
Flow Builder tests
Flow tests let you save a repeatable scenario with the flow. A test defines three things:
- An initial record, which is the triggering record on create, or the prior values on update.
- The updated values, for update flows.
- Assertions, which are conditions that must be true after the flow runs.
Typical assertions for the earlier before-save flow:
{!$Record.NextStep}equalsReview large deal.- For an opportunity with
Amountof 5,000,{!$Record.NextStep}is unchanged.
Flow tests are rolled back, so they don't leave data behind. They test the flow in isolation, the same way the debugger does. They don't replace tests of the whole transaction. Don't expect them to cover scheduled or asynchronous paths. Check those separately with the debugger and Apex tests.
Debug logs
When behavior differs between the debugger and a real save, use a debug log. Another trigger, a validation rule, or a second flow is usually the cause.
- In Setup, open Debug Logs and add a trace flag for the user who performs the save.
- Use a debug level that sets the Workflow category to
FINER. This level gives you element-level flow detail. - Save the record, then open the log.
Search the log for FLOW_. These events show the flow running:
FLOW_START_INTERVIEWS_BEGIN
FLOW_START_INTERVIEW_BEGIN Opportunity - Set Next Step
FLOW_RULE_DETAIL Entry condition result
FLOW_ELEMENT_BEGIN Assignment
FLOW_ASSIGNMENT_DETAIL {!$Record.NextStep} = Review large deal
FLOW_START_INTERVIEWS_ENDThe log also shows where the flow sits relative to Apex. Look at the CODE_UNIT_STARTED lines. Before-save flows appear before before-triggers finish and after-save flows appear after the record is saved, which confirms the order of execution you learned earlier. The log also shows the limit usage lines (LIMIT_USAGE_FOR_NS), so you can see how many SOQL queries and DML statements the flow shares with Apex in that transaction.
A flow that fails during a save produces an error to the user or caller. Admins also receive a flow error email, depending on the org's settings. The email names the flow, the element, and the error message. For failures in scheduled or asynchronous paths there is no user on screen, so the email and the log are often your only evidence.
Flow Trigger Explorer
Open Flow Trigger Explorer in Setup, pick an object, and see every record-triggered flow on it, grouped by trigger event and before or after save. You can set the order of flows that run on the same event. Use it when:
- Two flows set the same field and the later one overwrites the first.
- A flow that you didn't write changes values you expected to stay the same.
- You need to confirm that an inactive or draft flow is not part of the run.
Testing with Apex
Apex tests exercise the entire transaction. That includes your flow, triggers, validation rules, and workflow. Flows run during Apex DML if they are active. A test that depends on a flow fails if someone deactivates the flow, which is useful as a regression check.
This test checks the before-save flow from earlier:
@IsTest
private class OpportunityFlowTest {
@IsTest
static void beforeSaveFlowSetsNextStep() {
Opportunity opp = new Opportunity(
Name = 'Large deal',
StageName = 'Prospecting',
CloseDate = Date.today().addDays(30),
Amount = 200000
);
insert opp;
Opportunity saved = [SELECT NextStep FROM Opportunity WHERE Id = :opp.Id];
Assert.areEqual('Review large deal', saved.NextStep);
}
}Two more tests complete the picture. One checks the negative case. The other checks the after-save flow with 200 records, which is the batch size of one trigger chunk.
@IsTest
private class OpportunityFlowBulkTest {
@IsTest
static void smallDealIsLeftAlone() {
Opportunity opp = new Opportunity(
Name = 'Small deal',
StageName = 'Prospecting',
CloseDate = Date.today().addDays(30),
Amount = 5000
);
insert opp;
Opportunity saved = [SELECT NextStep FROM Opportunity WHERE Id = :opp.Id];
Assert.isNull(saved.NextStep);
}
@IsTest
static void closedWonCreatesOneTaskPerOpportunity() {
List<Opportunity> opps = new List<Opportunity>();
for (Integer i = 0; i < 200; i++) {
opps.add(new Opportunity(
Name = 'Bulk deal ' + i,
StageName = 'Prospecting',
CloseDate = Date.today().addDays(30)
));
}
insert opps;
for (Opportunity o : opps) {
o.StageName = 'Closed Won';
}
update opps;
Integer taskCount = [
SELECT COUNT() FROM Task
WHERE WhatId IN :opps AND Subject = 'Send welcome package'
];
Assert.areEqual(200, taskCount);
}
}The bulk test exposes limit problems. If the flow runs a Get Records or Create Records inside a loop, 200 records can hit the SOQL or DML limit. The test then fails with a limit exception.
The test inserts the opportunities in Prospecting and updates them afterward. The after-save flow runs only when a record is updated to meet the condition, so inserting at Closed Won would not start it.
Asynchronous paths in tests
Flow paths that run asynchronously start after the transaction commits. In a test, wrap the DML in Test.startTest() and Test.stopTest(). Asynchronous work completes when stopTest() returns, so you can assert on its results afterward. Scheduled paths wait for a time offset, so test their logic through the debugger. Don't try to wait for them in Apex.
Diagnosing common failures
| Symptom | Likely cause | Check |
|---|---|---|
| Flow never starts | Entry condition false, or old version active | Debug with `$Record__Prior` set. Confirm the active version |
| Works in debug, fails in real save | Validation rule, Apex trigger, or user permissions | Debug log with Workflow at `FINER` |
| Field value reverts | A later flow or trigger overwrites it | Flow Trigger Explorer and the log's order of events |
| Limit exception on bulk load | Element inside a loop | Bulk Apex test with 200 records |
| Task or child record missing | After-save flow didn't start on insert | Run frequency and trigger event |
Exercise
A before-save flow sets NextStep when Amount is over 100,000. It works in the Flow Builder debugger, but in production some users report that NextStep is blank after saving. Name two tools you would use to find the cause, and what you would look for in each.
Show answer
Use Flow Trigger Explorer to check whether another flow on Opportunity runs later and clears or overwrites NextStep. Use a debug log for an affected user with Workflow at FINER, and look at the FLOW_ events and the order of Apex triggers. The log shows whether the entry condition passed, whether a validation rule or trigger changed the field afterward, and whether a permission difference changed the flow's behavior.
Exercise
Why does the Closed Won bulk test insert the opportunities as Prospecting and then update them?
Show answer
The after-save flow runs only when a record is updated to meet the entry condition. Inserting the records already at Closed Won would not start the flow, so no tasks would be created and the test would fail for the wrong reason.