Skip to main content
Build the future with Agentforce at TDX in San Francisco or on Salesforce+ on March 5–6. Register now.

Debug Your Code

With Apex Replay Debugger configured, let’s do some testing and debugging to fix some Apex code.

Deploy Metadata to Org

  1. In Visual Studio Code, right-click the folder classes, then choose SFDX: Deploy Source To Org.
Note

If the SFDX: Deploy Source to Org option is unavailable, ensure you authorized your Trailhead Playground as an org with Salesforce CLI per the previous step.

Run Apex Tests

  1. In Visual Studio Code, click the View menu then choose Command Palette.... Alternatively, you can use the keyboard shortcut Ctrl+Shift+P (Windows or Linux) or Cmd+Shift+P (macOS) to open the Command Palette.
  2. Enter apex test in the search box, then choose SFDX: Run Apex Tests.
    Command Palette filtered to SFDX: Run Apex Tests command
  3. Choose AccountServiceTest.
    Apex test classes to invoke from Visual Studio Code
  4. Note the test result outcome in the Output panel. Uh oh, our Apex test failed! 😯
    Apex test result displaying failure message in Visual Studio Code

The error message indicates the wrong value was assigned to the account’s ticker symbol field. Let’s set a checkpoint in the code, rerun the test to collect a debug log, then replay the debug log to find our code bug 🐞.

Set Breakpoints and Checkpoints

When debugging, a breakpoint instructs the running program to pause at a specific line number so the developer can inspect variable values at that point in time. Checkpoints, a special feature for debugging Apex code, are a type of breakpoint that provides more information by capturing heap dumps. You can set as many breakpoints as you like, but you can only set up to five checkpoints at a time. Compared to breakpoints, checkpoints provide richer information for all local variables, static variables, and trigger context variables.

Use the Debug: Toggle Breakpoint and SFDX: Toggle Checkpoint commands to toggle on and off breakpoints and checkpoints, respectively. Breakpoints and checkpoints display an indicator next to the line number to show they’ve been set. To distinguish a breakpoint from a checkpoint, breakpoints display as a solid red dot and checkpoints display as a red circle with a line through the center.

  1. In Visual Studio Code, open the AccountService.cls file.
  2. Set a breakpoint by clicking to the left of the line number for the line with the Account newAcct = new Account(.
    Breakpoint set on line in AccountService.cls in Visual Studio Code
  3. Focus your cursor on the line with the return newAcct; statement.
    Cursor focused on line where to add checkpoint in AccountService.cls file in Visual Studio Code
  4. Open the Command Palette.
  5. Enter sfdx checkpoint in the search box, then choose SFDX: Toggle Checkpoint.
    Command Palette filtered to SFDX: Toggle Checkpoint command
    You should see an indicator next to the line number showing that the checkpoint was set.
    Checkpoint set on line in AccountService.cls in Visual Studio Code
  6. Open the Command Palette and enter sfdx checkpoint in the search box, then choose SFDX: Update Checkpoints in Org. You must tell Salesforce about your checkpoints so that heap dumps are collected as your Apex code executes. If you modify your Apex code or toggle checkpoints, run this command again to stay in sync.
    Command Palette filtered to SFDX: Update Checkpoints command
Note

You can set as many breakpoints as you like, but you can only set up to five checkpoints at a time. For this project, you must set at least the checkpoint mentioned above in AccountService.cls.

Be sure to generate debug logs and replay them with Apex Replay Debugger soon after updating your checkpoints, because checkpoints expire after 30 minutes and heap dumps expire after a day.

Run Apex Tests and Get Debug Logs

With our breakpoints and checkpoints set, it’s time to rerun our Apex test to generate a replay-enabled debug log.

  1. In Visual Studio Code, click the View menu then choose Command Palette.... Alternatively, you can use the keyboard shortcut Ctrl+Shift+P (Windows or Linux) or Cmd+Shift+P (macOS) to open the Command Palette.
  2. Enter sfdx replay in the search box, then choose SFDX: Turn On Apex Debug Log for Replay Debugger. This creates a trace flag to generate replay-enabled debug logs for 30 minutes. You can change the duration in Setup on the Debug Logs page.
    Command Palette filtered to SFDX: Invoke Apex Tests command
  3. Open the Command Palette and enter apex test in the search box, then choose SFDX: Run Apex Tests.
    Command Palette filtered to SFDX: Run Apex Tests command
  4. Choose AccountServiceTest.
    Apex test classes to invoke from Visual Studio Code
  5. We haven’t changed our Apex code, so we should expect the test to fail again. What has changed is that we now have a replay-enabled debug log and checkpoints to help us find and fix the bug 🐞.
    Apex test result displaying failure message in Visual Studio Code
  6. Open the Command Palette and enter sfdx get in the search box, then choose SFDX: Get Apex Debug Logs.... After a few seconds, you are prompted to select a debug log to download.
    Command Palette filtered to SFDX: Get Apex Debug Logs command
  7. Choose the debug log associated with the recent Apex test run; usually this is the first entry in the list. After a few seconds, Visual Studio Code opens the downloaded debug log.
    Debug Logs to download

Replay an Apex Debug Log

In this step, you replay the debug log that you recently downloaded. However, the debug log that you replay could easily have been generated and shared with you by another developer on your team.

Note

When replaying a debug log, make sure that your Salesforce DX project contains the same Apex source code that generated the debug log. Otherwise, line numbers and variables referenced by the debugger might lead you to confusion instead of to discoveries. The debug log must be generated with a log level of FINER or FINEST for log category Visualforce and a log level of FINEST for log category Apex Code.

  1. In Visual Studio Code, open the debug log downloaded in the previous step, if it’s not already open. You can find other debug logs you’ve downloaded with Visual Studio Code in the .sfdx/tools/debug/logs folder.
    Navigating to saved debug logs
  2. Right-click any line in the debug log, then choose SFDX: Launch Apex Replay Debugger with Current File. After a few seconds, Visual Studio Code opens the Debug sidebar, ready for you to begin stepping through the code.
    Launching Apex Replay Debugger in Visual Studio Code
  3. Click Continue button on Debug Toolbar on the Debug Toolbar to continue to the first breakpoint. If you set multiple breakpoints, continue to click Continue button on Debug Toolbar until the debugger arrives at the checkpoint for the return newAcct; statement in AccountService.cls.
    Debugging session started in Visual Studio Code
  4. Apex Replay Debugger pauses on the line with the return newAcct; statement in AccountService.cls, where you set the checkpoint in a previous step. The Debug sidebar displays the current variable values in scope. We confirm that the tickerSymbol argument passed to the createAccount method has the expected value 'CRM', so we know our code is passing in the correct value to the method. So far, so good. 🙂
    Debug session paused at line in AccountService.cls in Visual Studio Code
  5. In the Debug sidebar, expand the newAcct variable and note that the TickerSymbol property value 'SFDC' does not match the tickerSymbol argument value 'CRM' passed to the createAccount method. Interesting. 🤔
    Debug session paused at line in AccountService.cls in Visual Studio Code
  6. Inspecting the code of AccountService.cls we see there’s a typo—the code is assigning the wrong argument: TickerSymbol = accountNumber. We’ve found our bug! 🐞
  7. Click Stop button on Debug Toolbar on the Debug Toolbar to end the debugging session.

Deploy Fixed Metadata to Org

  1. Correct the code in AccountService.cls to assign the tickerSymbol argument to the TickerSymbolfield.
    TickerSymbol = tickerSymbol
  2. Save the file.
  3. Right-click any line in the file, then choose SFDX: Deploy This Source to Org.

Run Apex Tests to Verify Fix

With our updated code deployed, it’s time to rerun our Apex test to verify the fix.

  1. Open the Command Palette.
  2. Enter apex test in the search box, then choose SFDX: Run Apex Tests.
    Command Palette filtered to SFDX: Run Apex Tests command
  3. Choose AccountServiceTest.
    Apex test classes to invoke from Visual Studio Code
  4. Note the test result outcome in the Output panel. Hooray, our Apex test passed! 🥳
    Apex test result displaying pass message in Visual Studio Code

That’s it, you’re done! You just developed, tested, and debugged Apex code using Apex Replay Debugger and Salesforce Extensions for Visual Studio Code.

Click Verify Step to confirm the debug session with your hands-on org and to collect your badge.

Resources

Condividi il tuo feedback su Trailhead dalla Guida di Salesforce.

Conoscere la tua esperienza su Trailhead è importante per noi. Ora puoi accedere al modulo per l'invio di feedback in qualsiasi momento dal sito della Guida di Salesforce.

Scopri di più Continua a condividere il tuo feedback