Friday, January 18, 2019

Debugging Salesforce Tests with VS Code

Debugging in Salesforce is relatively new and it immature compared to debugging in Visual Studio or Eclipse.  Even Microsoft Access has a better debugger than Salesforce.  Essentially logs files are created when running tests and then the log files are used to step through the code.  It is after the fact debugging, you cannot change the path that the code has gone.

If you haven't already, set up VS Code for Salesforce


  1. Click the Debug icon
  2. In the drop down list at the top under Debug, Add Configuration.  Select Apex Replay Debugger
  3. Make sure that you have pulled all your code and tests down from source control or from your org.
  4. Using Ctrl-Shift-P on Windows or Command-Shift-P on a Mac, select SFDX: Turn On Apex Debug Log for Replay Debugger
  5. Set a break point in your test
  6. Run your test by clicking Run Test above the test method name in VS Code 
  7. Using Ctrl-Shift-P on Windows or Command-Shift-P on a Mac, select SFDX: Turn Off Apex Debug Log for Replay Debugger
  8. Using Ctrl-Shift-P on Windows or Command-Shift-P on a Mac, select SFDX: Get Apex Debug Logs
  9. Click the Debug icon
  10. Click the Launch Apex Replay Debugger
  11. You can see local variables on the left and you can step into code

Setting Up VS Code with Salesforce

These are step by step instructions to get VS Code working with Salesforce.


  1. Go to your Sandbox, under Setup search for Dev Hub.  Enable Dev Hub and GA.  This will allow you to create scratch orgs.
  2. Install the Salesforce CLI which will allow you to push and pull code and run tests:   https://developer.salesforce.com/tools/sfdxcli
  3. Install VS Code which is now the standard for editing code for Salesforce:  https://code.visualstudio.com/download
  4. Install Java if you do not have it installed yet.  The Salesforce Extension Pack requires it.  https://www.java.com/en/download/
  5. Install the Salesforce Extension Pack.  In VS Code, click the extensions icon and search for Salesforce Extension Pack.
  6. Install Lightning Web Components extension in VS Code
If your Salesforce code is in your org and not in source control then you are in Classic Development and not Salesforce DX.  

Classic Development Mode (directly interact with an Org)



* After creating the project, create these folders under the default folder before doing a Retrieve Source From Org:  

  • classes
  • objects

Salesforce DX Mode (your source is in GitHub or Bitbucket)

Example of cloning a repository, creating a scratch org and pushing the code

git clone https://github.com/dreamhouseapp/dreamhouse-sfdx
cd dreamhouse-sfdx
sfdx force:config:set defaultdevhubusername=your@username.com
sfdx force:org:create -a dreamhouse -s -f config\project-scratch-def.json -d 7
sfdx force:source:push
sfdx force:user:permset:assign -n dreamhouse
sfdx force:org:open -p /lightning/page/home