With Apex Replay Debugger configured, let’s do some testing and debugging to fix some Apex code.
Another option is to use the SFDX: Create Project With Manifest command in VS Code (equivalent to using sfdx force:project:create with the -x or -manifest option), which will create your project with a manifest/package.xml file, that includes wildcard selectors for all the custom code types: ApexClass, ApexComponent, ApexPage, ApexTestSuite, ApexTrigger, and AuraDefinitionBundle, as well as. Come learn how the Salesforce Extensions for Visual Studio Code will make you a more productive and (hopefully) happier developer. You will see all the latest features such as the no-cost Replay Debugger and improved testing capabilities. This session will be fast paced and loaded with demo, tips, and tricks that you can apply to your work immediately. Or at least after Dreamforce is over. Open an Apex Class. Open the Salesforce Query editor extension. Create/Write your query. Click anywhere in the Apex class to set the cursor/anchor where you want to place the query. If the cursor/anchor is anywhere inside a soql query (inside the ), the Update Apex button appears.
Deploy Metadata to Org
- In Visual Studio Code, right-click the folder
classes
, then choose SFDX: Deploy Source To Org.
Note
Salesforce Visual Studio Code
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
- 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.
- Enter
apex test
in the search box, then choose SFDX: Run Apex Tests. - Choose AccountServiceTest.
- Note the test result outcome in the Output panel. Uh oh, our Apex test failed! ?
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.
- In Visual Studio Code, open the
AccountService.cls
file and focus your cursor on the line with thereturn newAcct;
statement. - 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.
- Enter
sfdx checkpoint
in the search box, then choose SFDX: Toggle Checkpoint.
You should see an indicator next to the line number showing that the checkpoint was set. - 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.
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.
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.
![Salesforce visual studio code execute anonymous Salesforce visual studio code execute anonymous](/uploads/1/1/8/5/118526355/309582488.png)
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.
- 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.
- 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. - Open the Command Palette and enter
apex test
in the search box, then choose SFDX: Run Apex Tests. - Choose AccountServiceTest.
- 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 ?.
- 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. - 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.
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.
- 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. - 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.
- Click on the Debug Toolbar to continue to the first breakpoint. If you set multiple breakpoints, continue to click until the debugger arrives at the checkpoint for the
return newAcct;
statement in AccountService.cls. - 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 thetickerSymbol
argument passed to thecreateAccount
method has the expected value 'CRM', so we know our code is passing in the correct value to the method. So far, so good. ? - In the Debug sidebar, expand the newAcct variable and note that the
TickerSymbol
property value 'SFDC' does not match thetickerSymbol
argument value 'CRM' passed to thecreateAccount
method. Interesting. ? - Inspecting ? the code of AccountService.cls we see there’s a typo—the code is assigning the wrong argument to
TickerSymbol
. We’ve found our bug! ? - Click on the Debug Toolbar to end the debugging session.
Deploy Fixed Metadata to Org
- Correct the code in AccountService.cls to assign the
tickerSymbol
argument to theTickerSymbol
field. - Save the file.
- 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.
- 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.
- Enter
apex test
in the search box, then choose SFDX: Run Apex Tests. - Choose AccountServiceTest.
- Note the test result outcome in the Output panel. Hooray, our Apex test passed! ?
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
Install Salesforce Extensions for Visual Studio Code
Visual Studio Code is the go-to code editor for Salesforce developers. It's free, open-source, and available for Windows, Linux, and macOS. This editor has easy-to-install extensions for syntax highlighting, code completion, and more.
In this project, we install Visual Studio Code and the recommended Salesforce Extension Pack.
- Download and install the latest version of Visual Studio Code for your operating system. If you already have Visual Studio Code installed, there’s no need to reinstall it.
- Launch Visual Studio Code.
- On the left toolbar, click the Extensions icon .
- Search for
Salesforce Extension Pack
and click Install.
Note
Salesforce Visual Studio Code Retrieve Source
Some features, particularly Apex support, in Salesforce Extensions for Visual Studio Code depend on the Java Platform, Standard Edition Development Kit (JDK). Only JDK 8 and 11 are supported. If you have another version of Java installed, you need to also install JDK 8 or JDK 11. Once you install the right JDK, use the following links to configure your setup:
Ensure Your Development Environment Is Ready
Sfdx Vscode
Now that you’ve installed Visual Studio Code and enabled the necessary extensions, you need to test them out.
- In Visual Studio Code, open the Command Palette by pressing Ctrl+Shift+P (Windows) or Cmd+Shift+P (macOS).
- Enter
sfdx
to filter for commands provided by the Salesforce Extensions.
As you use more SFDX commands, those commands will show up in the
recently used
panel.In the final step, you create your first Lightning web component and add it to your org’s home page.
We won’t check any of your setup. Click Verify Step to go to the next step in the project.