Sub topic 13
Testing and Debugging Record-Triggered Flows
This chapter assumes you can build a record-triggered flow, know the difference between before-save and after-save, and understand entry conditions and $Record. It covers how to confirm that a flow does what you intended and how to find the cause when it doesn't.
Two ways to test
A record-triggered flow runs inside the save transaction of a record. You can test it in two ways.
- Flow Builder Debug runs the flow on a real record you pick, with options you set. It is quick feedback while you build.
- Apex tests insert or update records and assert the results. They are repeatable and run in deployments.
Use Debug while you build, and add Apex tests before you deploy.
Debugging in Flow Builder
Open the flow and click Debug. For a record-triggered flow, you choose these options:
- Select a record to use as
$Record. Pick an existing record. For an update trigger, you can also set Set the values of$Record__Priorso the flow sees a change. - Run the flow in rollback mode. Changes are rolled back when the debug run ends, so no data is saved. Leave this on unless you want real changes.
- Show details of what's in scope. The debug log lists each element, its inputs, and its outputs.
The debug log shows which path each Decision took, the value of each variable, and whether the entry condition passed. If the flow never starts, check the entry condition first. A condition like ISCHANGED({!$Record.StageName}) only passes when the prior value is different, so set $Record__Prior to an older stage when you debug.
Debug has limits. Rollback mode skips some actions, and scheduled and asynchronous paths are not fully simulated. Treat Debug as a check of your logic, not of your whole org.
Testing with Apex
An Apex test runs the active flow when it performs DML on the object. This test assumes the before-save flow "Opportunity - Set Next Step" is active, and it checks that a large Opportunity gets its NextStep set.
@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);
}
}The test queries the record after the insert. In a before-save flow the value is written during the save, so you read it back from the database to see it.
Test these cases for each flow:
- A record that meets the entry condition. The flow should act.
- A record that does not meet it. The flow should do nothing.
- An update that changes a field the condition watches, and one that does not.
- A bulk insert of 200 records. This catches queries or DML inside loops.
Running tests in a sandbox before deployment shows whether the flow works together with other automation on the same object.
Reading failures
When a flow fails, Salesforce can send an email with the flow name, the element that failed, and the error message. For failures you can't reproduce, use these tools:
| Tool | What it shows |
|---|---|
| Debug in Flow Builder | Element-by-element values for one record |
| Debug logs (Setup) | The full transaction, including flows, Apex, and limits |
| Flow error email | The failing element and message for a runtime failure |
| Apex test results | Whether the final data matches your assertions |
Common problems
- The flow doesn't run. The flow is inactive, the entry condition is false, or the run frequency is set to run only when the record meets the condition after a change.
- A field value is not what you expected. Another flow, trigger, or workflow rule changed it later. Check the order of execution in the debug log.
- A limit error appears. The flow shares limits with Apex in the same transaction. Look for Get Records or DML elements inside a loop.
- A test passes alone but fails in a deployment. The test depends on data in the org. Create all the data the test needs inside the test.
Exercise
Your before-save flow sets NextStep when Amount is above 100000. A test inserts an Opportunity with Amount 200000, but reading NextStep from the inserted variable opp returns null. The same field read from a fresh query is correct. Why, and how do you fix the test?
Show answer
The flow writes the value during the save, and the in-memory opp variable was not refreshed with it. Query the record by Id after the insert, as in [SELECT NextStep FROM Opportunity WHERE Id = :opp.Id], and assert on the queried record.
Exercise
Why should a test for a flow include a record that does not meet the entry condition?
Show answer
It confirms the flow does nothing when it shouldn't. This guards against an entry condition that is too broad and fires on records it should skip.