top of page
Writer's pictureS. Yadav

How to use Apex replay debugger extension in SF for debugging


Debugging is crucial to Apex development, ensuring your code is error-free and functions as intended. The Apex Replay Debugger is a powerful tool that allows you to debug your Apex code easily and efficiently. Whether you are new to debugging or looking to streamline your debugging process, this guide will help you harness the full potential of the Apex Replay Debugger.


Apex Replay Debugger

The Apex Replay Debugger is an advanced tool that helps Salesforce developers troubleshoot Apex code by visually replaying code execution using debug logs. These logs are generated by Salesforce when a transaction runs and capture the finest details of the code, including variable states, SOQL query results, method entries and exits, and more. When the debug level is set to Finest, the log captures a detailed trace of the entire code execution, similar to how a browser’s developer tools (like Chrome Debugger) capture website activity. The Apex Replay Debugger in Visual Studio Code then uses this information to visually represent the code flow, allowing you to inspect every aspect of the transaction: API requests and responses, heap dumps, and variable values, providing a comprehensive view of your code's behavior.


Setting Up the Apex Replay Debugger

Before you can start debugging, you must set up the necessary configuration. Create a launch.json file in your project with the following configuration:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Apex Replay Debugger",
            "type": "apex-replay",
            "request": "launch",
            "logFile": "${command:AskForLogFileName}",
            "stopOnEntry": true,
            "trace": true
        }
     
    ]
}

Setting Breakpoints and Checkpoints

Breakpoints: 

These can be set on the fly in your .cls or .trigger files by clicking the column to the left of the line numbers. They help pause the execution of your code at specific lines.


Checkpoints: 
Debugging with Apex Replay Debugger

Now let's go through a practical example to illustrate the debugging process.

Consider the following Apex class:

Public with sharing class ApexReplayDebugger {
    public static Integer calculateSum(List<Integer> numbers) {
        Integer sum = 0;
        for (Integer num : numbers) {
            sum += num;
        }
        return sum;
    }

    public static void apexDebugger() {
        List<Integer> numbers = new List<Integer>{1, 2, 3, 4, 5};
        Integer sum = calculateSum(numbers);
        System.debug('Sum: ' + sum);
    }
}

These are snapshots of all objects in your org at a specific time. Unlike breakpoints, checkpoints provide detailed heap dumps, including all local, static, and trigger context variables. To set checkpoints, follow these steps:


  1. Click the line of code where you want to set the checkpoint.

  2. Open the Command Palette (Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on macOS).

  3. Run SFDX: Toggle Checkpoint.

  4. Alternatively, right-click in the gutter, select Add Conditional Breakpoint | Expression, and set the expression to Checkpoint.


You can see the red dot on the line number where you added the checkpoints.


After setting checkpoints, upload them to your org by running SFDX: Update Checkpoints in Org from the Command Palette.


You get this on output.

You can see that checkpoints appear in your org.

Note: If you cannot see checkpoints on your class in your org, deploy your class from vs code to source org again.


Running the Debugger

Run the Class: Execute the class from the anonymous window using ApexReplayDebugger.getCalculation();


Turn On Apex Replay Debugger:

Open the Command Palette (Ctrl+Shift+P on Windows/Linux, Cmd+Shift+P on macOS).

Run SFDX: Turn On Apex Replay Debugger.

After clicking on turn on Apex replay debug you can see something like this in the output screen.


The debug level you see here reflects your dev console debug level but now we have debugLevelId to update it by running commands.


Here we update the visual force debug level from INFO to FINEST.


Update the Visualforce debug level to FINEST.

Use the following command to modify the DebugLevel object to set Visualforce to FINEST:

sf data:update:record --sobject DebugLevel --record-id 7dlH40000008ckSIAQ --values Visualforce=FINEST --use-tooling-api --json

Verify the Update If the command succeeds, you will see output like this:

{
  "status": 0,
  "result": {
    "id": "7dlH40000008ckSIAQ",
    "success": true,
    "errors": []
  },
  "warnings": []
}

Or you can see from the dev console 

Go to the dev console, click debug, and change the debug level.

Click on Change/Add to your debug level. 

Get Apex Debug Logs:

Run SFDX: Get Apex Debug Logs to see a list of recent debug logs.

Select the latest debug log.


      Get the list of debug logs. 


Launch the Debugger:

Run SFDX: Launch Apex Replay Debugger with the current file.

You can now inspect the debug log and analyze the captured breakpoints. 

Click the down arrow and up arrow to check the out[ut of the breakpoints.


Inside you can see the output 


Conclusion

The Apex Replay Debugger is a vital tool for Salesforce developers. It enables efficient debugging with comprehensive variable inspection capabilities. You can ensure your Apex code is robust and error-free by mastering breakpoints, checkpoints, and the replay debugging process. 


If you'd like to see the code and resources used in this project, you can access the repository on GitHub. To access the AVENOIRBLOGS repository, click here. Feel free to explore the code and use it as a project reference.


Happy Debugging! Please leave a comment to help me understand how the blog helped you. If you need further assistance, please contact us. You can also click "Reach Us" on the website and share the issue with me.


Reference



Blog Credit:

S. Yadav

Salesforce Developer

   Avenoir Technologies Pvt. Ltd.

  Reach us: team@avenoir.ai


Recent Posts

See All

Comments


bottom of page