Sub topic 12
Putting Together a Debugging and Testing Strategy
What this chapter assumes
This chapter combines the techniques from earlier chapters into one routine. It assumes you can already do the following:
- Build a flow in Flow Builder and run it with the debugger.
- Add a fault path to an element.
- Read a debug log.
- Write a basic Apex test.
You will also need a sandbox or Developer Edition org. Run all of the work below there, not in production.
The routine at a glance
Each technique answers a different question. Debug mode asks "does this path do what I intended?" Flow tests and Apex tests ask "does it still work after someone changes something?" Bulk tests ask "does it survive 200 records?" Fault paths and error emails ask "what happens when it breaks in production?" A good strategy uses them in order.
Step 1: Build for diagnosis
Add fault paths while you build. Do not wait until something fails. On every element that touches data (Create, Update, Delete, Get Records, Action), connect a fault path that records {!$Flow.FaultMessage} and then stops or shows a clear message. Keep queries and DML outside loops. Collect records in a collection variable, then run one Update or Create after the loop. This single habit prevents most governor limit failures.
Step 2: Debug each path by hand
Run the flow in Flow Builder debug mode. Walk each branch of every decision at least once. Use the input values and the "run as another user" option to cover the different people who will trigger the flow. Use rollback mode when you do not want the test to leave changes in the sandbox. Check the element-by-element results for the actual values of variables, not only for the absence of errors.
Step 3: Turn the manual checks into tests
A manual debug run is not repeatable. Once a path works, capture it as a test:
- Flow Builder tests cover record-triggered flows. Set the initial record values and the updated values, then add assertions on the result. Create one test for each important decision outcome.
- Apex tests cover what Flow Builder tests cannot reach easily. Examples are records created in other objects, flows started with
Flow.Interview, and interactions with other automation such as triggers. - Bulk tests are Apex tests that change 200 records in one transaction. A flow that passes with one record can still fail on a batch.
Step 4: Monitor after release
Tests reduce failures but do not remove them. Production data and other automation will surface cases you did not test. Make sure error emails reach someone who will read them, and check the failed interviews list in Setup on a regular schedule. Use the fault path output to find which record and element failed.
Choosing the right tool
| Situation | Best first tool |
|---|---|
| A new flow does not do what you expect | Debug mode in Flow Builder |
| A flow fails only for one type of user | Debug mode, running as that user |
| A failure in production with no clear cause | Debug log, then the failed interview details |
| A change might break existing behavior | Flow test or Apex test |
| "Too many SOQL queries" or "Too many DML statements" | Bulk test, then look for loops that contain data elements |
| Users need to know a flow failed | Fault path with a message or email |
Working through a failure
When a test or user report fails, work in this order:
- Reproduce it with the same record values in debug mode.
- Find the failing element from the debug results or the error message.
- If the cause is not visible, set up a debug log for the user and read the flow section.
- Fix the flow.
- Add a test that fails without the fix and passes with it.
Step 5 is the one most often skipped. It stops the same bug from returning.
A short checklist
Before you activate a flow, confirm each of these:
- Every data element has a fault path.
- No Get Records, Create, Update, or Delete element sits inside a loop.
- Every decision outcome has been run in debug mode.
- Flow tests or Apex tests cover the main outcomes.
- A 200-record test passes.
- Someone receives the error emails.
Exercise
A record-triggered flow works for one record in debug mode and in your Flow Builder test. After release, users who import 200 records at once get a "Too many SOQL queries" error. Which technique in this chapter would have caught the problem, and what is the likely cause in the flow?
Show answer
A bulk test that updates or inserts 200 records in one transaction would have caught it. The likely cause is a Get Records element inside a loop, which runs one query for each record. The fix is to move the query outside the loop, or to query once and look up values in a collection, and then to keep the bulk test as a regression test.