loader
tenetizer-salesforce

Introduction:

The Apex Debugger, also known as the Apex Interactive Debugger, is an indispensable tool for Salesforce developers using Visual Studio Code. This powerful debugger enables real-time debugging of Apex code, allowing developers to identify and resolve issues efficiently. In this comprehensive guide, we’ll explore the key features, setup instructions, best practices, and considerations associated with the Apex Debugger.

Key Features of Apex Debugger:

The Apex Debugger offers a range of essential features to facilitate seamless debugging:

Breakpoint Management:

Create breakpoints in triggers and Apex classes to halt execution at specific points in your code.
Set breakpoints on get or set methods, ensuring precise control over code execution.

Variable and Call Stack Inspection:

View variables, including collections, Apex System types, and sObject types, to understand their values during execution.
Explore the call stack to analyze triggered variables, Apex Data Manipulation Language (DML) impacts, and method-to-method calls.

Global Class Interaction:

Interact with global classes, exceptions, and triggers of managed packages, even those not directly visible to you.
Gain insights into global variables during object inspection within managed types.

Traditional Debugging Techniques:

Utilize conventional debugging techniques, including running to breakpoints, stepping into, over, and out of code segments.
Receive debugging results in the Debug Console, enhancing the visibility of execution details.

Setup Instructions:

Follow these step-by-step instructions to set up and configure the Apex Debugger for effective debugging:

Scratch Org Configuration:

Add the “DebugApex” feature to the definition files for all scratch orgs you intend to debug.

Use SFDX in Visual Studio Code to create a default scratch organization with the DebugApex feature.

User Permissions:

Grant access to the Apex Debugger by creating a permission set in the scratch org.

Assign the permission set to the admin user of the org, enabling access to debugging functionalities.

Visual Studio Code Configuration:

Create an Apex Debugger launch configuration in Visual Studio Code by adding a “Launch Apex Debugger” configuration to the “configurations” array in launch.json.

Optional: Source Control Integration:

If using source control, add the newly created permission set to your repository for streamlined permission management.

				
					"configurations": [
  {
    "name": "Launch Apex Debugger",
    "type": "apex",
    "request": "launch",
    "sfdxProject": "${workspaceRoot}"
  }
]

				
			

Exception Breakpoints:

Enhance debugging precision by setting breakpoints on exceptions. Force the Apex Debugger to pause execution when errors occur, allowing focused issue identification and resolution.
Access the Command Palette (Ctrl+Shift+P) and choose “SFDX: Configure Apex Debug Exceptions.”
Choose an exception from the list and set it to “Always break” for precise debugging.
View and manage exception breakpoints using the “SFDX: Configure Apex Debug Exceptions” command.

User and Request Filters:

Filter debugging sessions based on users or request types to narrow your attention to critical events. Customize your launch configuration with the following filters:

				
					
"configurations": [
  {
    "name": "Launch Apex Debugger",
    "type": "apex",
    "request": "launch",
    "sfdxProject": "${workspaceRoot}",
    "userIdFilter": [],
    "requestTypeFilter": [],
    "entryPointFilter": ""
  }
]

				
			

Use regular expressions for “entryPointFilter” to filter by entry point, enhancing focus on specific events.

Considerations:

Be aware of certain restrictions and known difficulties when using the Apex Debugger:

General Considerations:

Avoid saving code modifications during an active debugging session to prevent discrepancies with breakpoints.

End debugging sessions properly to avoid orphaned sessions; use “SFDX: Stop Apex Debugger Session” in VS Code.

Entry Point Limitations:

Code running asynchronously, including asynchronous tests, is not accepted.
Apex Inbound email code for batch, queue, and scheduled messages with the @future annotation is excluded.

Breakpoint and Variable Considerations:
Conditional breakpoints are not supported.
Breakpoints on get or set methods must be within the body of the method.
Execute Anonymous blocks cannot have breakpoints, but the frame is displayed in the stack for inspection.

Variable Inspection Limitations:
Variables are not watchable, and dynamic Visualforce and Lightning components do not support variable inspection.
Drill into variables to view nested values; limitations exist for certain Apex library objects.

Conclusion:

The Apex Debugger is an invaluable tool for Salesforce developers, providing real-time insights into Apex code execution. By mastering its features, following proper setup procedures, and considering best practices, developers can streamline their debugging workflows and efficiently resolve issues in their code. Elevate your debugging experience with the Apex Debugger and unlock a new level of efficiency in Salesforce development. For detailed knowledge about implementation contact us through form below.