Sub topic 12
Debugging with the Apex Replay Debugger
This chapter assumes you have a working DX project with an authorized default org, and that GreetingService and its test class are deployed. It also assumes you can run anonymous Apex from VS Code. If a command below is missing from the Command Palette, revisit the extension pack and Java chapters first.
How replay debugging works
The Apex Replay Debugger does not attach to a running org. It reads a debug log that the org already wrote and lets you step through that log as if the code were running. You set breakpoints in your local source, and the debugger matches them to lines in the log.
This has three consequences:
- You must run the code first, then debug the log afterward.
- The log must have been captured at a detailed enough level, or variables will be missing.
- The deployed code must match your local files. If it doesn't, breakpoints land on the wrong lines.
Walkthrough
Use GreetingService from earlier chapters. Open GreetingService.cls and click in the gutter next to return 'Hello, ' + name + '!'; to set a breakpoint.
- Open the Command Palette and run SFDX: Turn On Apex Debug Log for Replay Debugger. This creates a trace flag for your user with a debug level that records variable values. The trace flag expires, so repeat this step in later sessions.
- Create a scratch file and enter this snippet:
System.debug(GreetingService.greet('Ada'));- Select the text and run SFDX: Execute Anonymous Apex with Currently Selected Text. The code runs in the org and produces a log.
- Run SFDX: Get Apex Debug Logs... and pick the newest log. VS Code downloads it and opens it.
- With the log file active, run SFDX: Launch Apex Replay Debugger with Last Downloaded Log File. You can also use ...with Current File.
Execution pauses at your breakpoint. The Variables panel shows name as Ada. Use Step Over, Step Into, Step Out, and Continue as in any VS Code debugger.
Debugging a test
You can also debug a test run. In GreetingServiceTest.cls, use the Debug Test code lens above a test method. The extension turns on logging, runs the test, fetches the log, and launches the debugger for you. This is the fastest path when a test fails and you want to see why.
What the debugger can and cannot do
| You can | You cannot |
|---|---|
| Set and remove breakpoints | Change variable values |
| Step through the logged execution | Run new code or evaluate expressions against the live org |
| Inspect variables, the call stack, and the heap as recorded | Pause a running transaction |
| Use it in any org, including production, with no extra license | Step through a log that was truncated or lacks the right level |
Common errors
- Breakpoint never hits. The log probably came from a different run than you expect, or the line did not execute. Confirm the log shows the class name and line in its
CODE_UNIT_STARTEDandLINEevents. - Variables are missing or empty. The trace flag was not set for replay, or it expired. Run the "Turn On" command again and rerun the code.
- Breakpoint lands on the wrong line. Your local file differs from the deployed version. Deploy again, rerun, and fetch a fresh log.
- "No logs found." The execution ran under a different user than the one with the trace flag.
Exercise
You set a breakpoint, ran a test, and launched the Replay Debugger, but the Variables panel shows no values. Name two likely causes and how you would fix each.
Show answer
The trace flag was not turned on for replay logging, or it expired. Run SFDX: Turn On Apex Debug Log for Replay Debugger, rerun the test, and fetch the new log. Another cause is a local file that differs from the deployed code, or a truncated log. Redeploy, rerun with a narrower scope, and fetch a fresh log.