Sub topic 16
Debugging and Monitoring Runs
This chapter assumes you can build an autolaunched flow and a schedule-triggered flow, and that you know what a fault path is. It covers how to test a flow before it goes live, how to find out what a live run did, and how to track down failures. The chapter is about 1,100 words.
Two different jobs
Debugging and monitoring answer different questions.
- Debugging is done before or during development. You run a flow on purpose and watch each element.
- Monitoring is done after activation. You look for evidence of what happened on runs you did not watch.
Autolaunched and scheduled flows have no screen, so no user sees an error as it happens. Failures show up as emails, as log entries, or not at all. You have to look for them.
Debugging in Flow Builder
The Debug button in Flow Builder runs the open version of the flow. The debug panel lists every element the run visited, the values of resources at each step, and the final outcome.
For an autolaunched flow, the debug panel asks you for values for each input variable. Enter realistic values, such as a real Account ID from the org. After the run, the panel shows the output variables.
For a schedule-triggered flow, debug runs the flow for the records that match the start element's filters. The options include:
- Run flow as another user. Use this to check how field-level security and sharing change the result. Without it, the flow runs as you.
- Show details of what was executed. Turns on a per-element trace.
- Rollback mode. Undoes the record changes when the debug run ends. Leave it on while you learn what a flow does.
Rollback mode and what it does not undo
Rollback mode only reverses database changes. Callouts, emails, and other external actions can still happen. Check which actions your flow uses before you debug against a real org.
Rollback mode is also not available for every flow type and may be unavailable when the flow contains certain actions. If the option is missing, assume that debug changes are permanent.
Reading a debug trace
Read the trace from top to bottom and compare it with what you expected. Check these points in order:
- Start element. Did the right records or input values enter the flow?
- Decision outcomes. Which outcome was taken, and which values caused it?
- Loops. How many iterations ran? A loop that runs zero times is often caused by an empty collection from a Get Records element.
- Data elements. Did Get Records find anything? Did Update Records report success?
- Fault path. Did the run leave through a fault connector? The trace shows the error message.
Two common surprises. A Get Records element that finds nothing is not an error, so the flow carries on with an empty variable. And a Decision that falls to the default outcome is also not an error, which can hide a bad condition.
Adding your own checkpoints
A trace only shows what exists. When you need more, add visibility to the flow.
- Assign a text variable at key points, such as
debugNote, and show it in the trace. - Write to a log object. Create a custom object, for example
Flow_Log__c, with fields for flow name, message, and record ID. Create a record from the fault path and from important steps. - Send an email action to an admin from the fault path.
Remember that log records created in a flow that later fails and rolls back are lost along with the rest of the transaction. A fault path that handles the error lets the transaction continue, so the log record survives.
Monitoring live runs
After activation, use these places in Setup.
| Place | What it shows | Typical use |
|---|---|---|
| Paused and Failed Flow Interviews | Interviews that paused or failed with an error | Finding failed runs that you can resume or delete |
| Scheduled Jobs (Setup, Scheduled Jobs) | Upcoming scheduled runs, including schedule-triggered flows | Confirming that a schedule exists and when it next fires |
| Apex Jobs | Batch-style processing for scheduled flows | Seeing whether a run is queued, running, or finished |
| Debug Logs | Detailed execution log for a traced user | Deep investigation of one run |
| Flow error emails | Messages sent to the person who last modified the flow | Learning about unhandled failures |
Menu names vary by release, so use Quick Find in Setup if a name has moved.
Flow error emails
When a flow fails with an unhandled error, Salesforce emails the user who last modified the flow. The email names the flow, the element that failed, the error text, and often a link to the interview. If that person has left the company, nobody sees the failures. For important flows, set up a fault path that notifies a monitored mailbox or queue.
Scheduled runs
A schedule-triggered flow does not run in your session. It starts at the scheduled time, as a background job, and runs as the Automated Process user unless the org setup says otherwise. To check on it:
- Open Scheduled Jobs and confirm the flow is listed with the next run time you expect.
- After the run time passes, open Apex Jobs and look for the matching job and its status.
- Check the data itself. Did the field the flow updates change on the expected records?
- Look for flow error emails.
If the flow is missing from Scheduled Jobs, check that the active version is the one with the schedule you set. A new version needs to be activated before its schedule takes effect.
Debug logs for a single run
Debug logs record what happened in a transaction at a chosen level of detail. To use one:
- In Setup, open Debug Logs.
- Add a trace flag for the user the flow runs as. For scheduled flows, this is usually the Automated Process user.
- Set the log level.
FINERorFINESTfor the Workflow category shows flow element detail. - Trigger the run and open the log.
In the log, search for FLOW_ELEMENT_BEGIN and FLOW_ELEMENT_END, which mark each element, and for FLOW_BULK_ELEMENT_BEGIN, which shows elements that ran on a batch of records. Fault paths appear with their error text.
Logs fill up fast and old ones are deleted. Set a short trace duration and remove the trace flag when you are done.
// Anonymous Apex: run an autolaunched flow while a trace flag is active.
Map<String, Object> inputs = new Map<String, Object>();
inputs.put('accountId', '001000000000000AAA');
Flow.Interview.Close_Stale_Cases interview = new Flow.Interview.Close_Stale_Cases(inputs);
interview.start();
System.debug('closedCount = ' + interview.getVariableValue('closedCount'));Run this from the Developer Console's anonymous Apex window and the log shows both the flow trace and your System.debug line.
Where the flow runs: a monitoring flow chart
The diagram shows how to decide where to look when a run did not do what you expected.
Common mistakes
- Debugging only with admin permissions. The flow may work for you and fail for the user it runs as. Use Run flow as another user.
- Treating "no error" as "worked". An empty Get Records result or a default Decision outcome produces no error. Check the data.
- Testing a schedule by waiting. Debug the logic with the same filter, then check the schedule separately in Scheduled Jobs.
- Leaving the notification to chance. Unhandled failures go to whoever last saved the flow.
- Debugging an inactive version. Debug runs the version you have open. Live runs use the active version. Confirm they match.
Exercise
A schedule-triggered flow updates Opportunities nightly at 02:00. Yesterday's run did not change anything, and nobody got an error email. List the steps you would take, in order, to find the cause.
Show answer
First, open Scheduled Jobs and confirm the flow is listed with the right next run time. If it is missing, the version with the schedule may not be active. Second, open Apex Jobs and check whether a job ran at 02:00 and what its status was. Third, check whether any records matched the start element's filter, since zero matching records is not an error. Fourth, run Debug in Flow Builder with Run flow as another user set to the Automated Process user to see whether permissions blocked the update. Finally, add a trace flag for that user and read the debug log for FLOWELEMENTBEGIN entries on the next run.
Exercise
Why might a log record created in a flow be missing after the flow failed, and how do you keep it?
Show answer
If the error is unhandled, the whole transaction rolls back, including the log record. Route the failing element to a fault path. The fault path handles the error, so the transaction can continue and the log record is saved.