Sub topic 12
Debugging with the Apex Replay Debugger
This chapter assumes you have a working Salesforce DX project, an authorized default org, and the GreetingService class deployed to that org. It also assumes you know how to run anonymous Apex from the previous chapter. You should be comfortable with breakpoints in any editor.
What the Replay Debugger is
The Apex Replay Debugger lets you step through a debug log in VS Code as if the code were running live. It does not attach to a running transaction. The transaction has already finished, and the debugger reads the log that the org wrote and walks you through it.
This has three consequences:
- You can debug in any org where you can generate a log, including production, because nothing is paused on the server.
- You cannot change variables or skip lines. You can only inspect what happened.
- The log is the only source of truth. If a line is missing from the log, the debugger cannot show it.
The debugger needs the log to contain enough detail to rebuild variable values and the call stack. VS Code produces such a log when you launch it through the Replay Debugger commands. A log captured some other way may be missing that detail.
Prerequisites inside VS Code
The Replay Debugger ships in its own extension, Apex Replay Debugger, which is part of the Salesforce Extension Pack. Confirm it is installed by opening the Extensions view and searching for Apex Replay Debugger. It needs the same Java runtime as the Apex language server, so a Java problem that breaks code completion can also break replay.
Your user must be allowed to view debug logs and to create trace flags in the org. A trace flag tells the org which user's activity to log and at what detail. If you work in a Developer Edition org as the administrator, you already have this access.
A class worth debugging
The tutorial's GreetingService is short, so it is a good first target. Here it is again, with the branch you will step through.
public with sharing class GreetingService {
public static String greet(String name) {
if (String.isBlank(name)) {
return 'Hello, world!';
}
return 'Hello, ' + name + '!';
}
}Make sure the current version is deployed. If you changed it since the last chapter, deploy it first:
sf project deploy start --source-dir force-appThe replay shows the code that ran in the org. If your local file differs from the deployed one, breakpoints will land on the wrong lines.
Step 1: Set a breakpoint
Open GreetingService.cls and click in the gutter, to the left of the line number, on the line if (String.isBlank(name)) {. A red dot appears. You can also place the cursor on the line and press F9.
Breakpoints in Replay Debugger sessions are checked against the log. A breakpoint on a line that never ran stays unreached. A breakpoint on a blank line or a comment is never hit.
Step 2: Create the log with anonymous Apex
Create a file such as scripts/apex/hello.apex and add the snippet from the previous chapter:
System.debug(GreetingService.greet('Ada'));Anonymous Apex is code the org compiles and runs once without saving it. It is the fastest way to trigger the class you want to debug.
With the file open, open the Command Palette (Ctrl+Shift+P, or Cmd+Shift+P on macOS) and run SFDX: Launch Apex Replay Debugger with Current File. VS Code does the following in order:
- It checks for a trace flag for your user and creates one if needed.
- It runs the anonymous Apex in the default org.
- It downloads the resulting log into
.sfdx/tools/debug/logsin your project. - It starts a debug session and opens the log.
The debugger opens, and execution stops at your first breakpoint. If it does not stop, see the troubleshooting section below.
Step 3: Step through the code
When the session pauses, look at three areas of VS Code.
- The Variables panel shows local variables and arguments. Expand
Localsto seenamewith the value'Ada'. - The Call Stack panel shows the chain of calls. You should see the
GreetingService.greetframe on top of the anonymous block. - The Debug toolbar has Continue (F5), Step Over (F10), Step Into (F11), and Step Out (Shift+F11).
Press F10 to step over the if line. Because name is 'Ada', String.isBlank returns false, and the next line to run is the final return. The return 'Hello, world!' line is skipped. Press F5 to continue to the end of the log. The session closes when the log is fully replayed.
Now change the call to GreetingService.greet('') and launch the debugger again. This time the debugger enters the if block and stops on return 'Hello, world!'. Running both paths side by side is a good way to confirm the branch logic.
Two ways to start a session
The Launch ... with Current File command is one of several entry points. The table lists the common ones.
| Command | Use it when |
|---|---|
| `SFDX: Launch Apex Replay Debugger with Current File` | The open file is an anonymous Apex script, an Apex test class, or a downloaded log file. |
| `SFDX: Launch Apex Replay Debugger with Last Run Test Method` | You just ran a test and want to see why it failed. |
| `SFDX: Get Apex Debug Logs` | You want to pick an existing log from the org, then replay it. |
For test classes, you can also use the debug links that appear above each test method in the editor (Debug Test) or in the Testing view. In an org that supports it, these use the Replay Debugger and generate the log for you.
Replaying a log that already exists
Not every log comes from your own anonymous script. A user may have hit a bug in the org, or a trigger may have run during a data load. To replay such a log:
- Run
SFDX: Get Apex Debug Logsfrom the Command Palette. - Pick a log from the list. The list shows the user, operation, time, and size.
- VS Code saves the log in
.sfdx/tools/debug/logs. - With the log file open, run
SFDX: Launch Apex Replay Debugger with Current File.
The debugger only knows source for classes in your project. If the log runs a class that is not in force-app, the replay cannot show those lines. Retrieve the class first, using the techniques from the earlier deploy and retrieve chapter.
Log levels and why replay sometimes looks empty
Debug logs have a level for each category, such as Apex Code, Database, and Workflow. The Replay Debugger needs the ApexCode category at FINEST and the Visualforce category at FINER to rebuild variable values and heap data. When you launch through the Replay Debugger commands, VS Code applies a suitable trace flag and debug level for you.
If you start from a log made by a different trace flag, you can get a replay with these symptoms:
- Variables show no values, or fewer values than expected.
- Some lines are skipped.
- The Call Stack lacks frames.
The fix is to generate the log again through the Replay Debugger command, or to edit the trace flag's debug level to raise Apex Code to FINEST.
Higher log levels have a cost. Logs have a size cap of 20 MB each, and the org keeps a limited total of log data. A long transaction at FINEST can reach the cap, and the org truncates the log. When that happens, the debugger can show only the part that was kept, so the end of the transaction is missing.
Checkpoints, and what the Replay Debugger cannot do
The Replay Debugger is not the same as the interactive debugger. The table compares them.
| Capability | Apex Replay Debugger | Apex Interactive Debugger |
|---|---|---|
| Works on a finished log | Yes | No |
| Pauses live code in the org | No | Yes |
| Edit variable values while stopped | No | Yes |
| Needs a special org license | No | Yes |
| Works in production | Yes, with log access | No |
Other limits to keep in mind:
- It replays only Apex. Flow, Process Builder, and Lightning component code do not appear as steppable lines.
- Step Into on a platform method such as
String.isBlankdoes not enter its code, because that source is not in your project. - You cannot move backward. If you step past the line you wanted, relaunch the session.
- Breakpoints you set while a session is running apply to the replay and can be hit if the log reaches them.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Command not found in the palette | Extension not installed or not enabled | Install the Salesforce Extension Pack and reload the window. |
| Session ends with no stop | Breakpoint on a line that did not run, or an old local file | Redeploy the class and put the breakpoint on a line that runs. |
| Error about Java | Missing or wrong JDK | Set `salesforcedx-vscode-apex.java.home` and reload. |
| No trace flag created | User lacks permission | Ask an administrator to grant debug log permissions. |
| Variables are empty | Log level too low | Relaunch with the Replay Debugger command. |
| Log not found | Wrong default org | Run `sf org list` and confirm the default org. |
A short test-driven loop
You can combine what you have learned. This loop is useful when a test fails and the assertion message is not enough.
- Run the test class from the editor and read the failure message.
- Set a breakpoint in the production class, on the line you suspect.
- Run
SFDX: Launch Apex Replay Debugger with Last Run Test Method. - Step through the code and compare the variable values with what the test expected.
- Fix the code, deploy, and run the tests again.
From the terminal, the last step looks like this:
sf project deploy start --source-dir force-app
sf apex run test --test-level RunLocalTests --result-format human --wait 10Exercise
You set a breakpoint inside the if block of GreetingService.greet, then launch the Replay Debugger on a script that calls GreetingService.greet('Ada'). The session finishes without stopping. Why, and what do you change to make it stop?
Show answer
The if block only runs when name is blank, and 'Ada' is not blank, so that line never appears in the log. Change the script to call GreetingService.greet('') (or move the breakpoint to the final return line), then launch the Replay Debugger again.
Exercise
A teammate says the Replay Debugger shows empty variable values for a log you downloaded with SFDX: Get Apex Debug Logs. Name one likely cause and one fix.
Show answer
The log was probably created under a trace flag with a low Apex Code level, so the log lacks the detail needed to rebuild variables. Either generate the log again with a Replay Debugger launch command, or raise the Apex Code level to FINEST on the trace flag and reproduce the transaction.