Debugging in SFDX

OK, so I’m starting to learn about debugging in SFDX.

Execute Anonymous

The easiest thing you can do is Execute Anonymous.

Here is the CLI reference for Execute, but as for all the Developer docs it’s hard to read and you just get lost.

Quick way:

  • In VS Code if you have created a Project with Manifest there is a hello.apex file in /scripts/apex folder.

  • Open that, edit the file, save the file, and then do ctrl+shift+p and type execute to select the SFDX: Execute Anonymous Apex with Editor Contentscommand.

  • That will run the code (remembering it is doing live DML in your org!)

  • The full log file will display in the output window.

  • There is no easy way to just filter like you do in the dev console

  • So do ctrl+f and type USER_DEBUG to see all the Debug lines.

More complex way, but cool:

  • in Terminal type sfdx force:apex:execute

  • Wait for the command prompt to return

  • Type an execute statement

  • Or paste in your code you want to run

  • do ctrl+d to run the code

  • The log is shown. Use the find command to find the Debug statements if you need.

Streaming Logs

Just like you would start a debug log in the UI, then do your stuff, then open the debug log and search through it, you can use the Streaming Logs..

  • Kier Bowden has a good blog post to explain it.

  • And another post that explains it.

  • And the Developer Reference.

  • There is some stack exchange questions talking about this further for more advanced things like setting logging levels etc…

You can do cool things like…

  • Adding colours to your debug logs

  • Filter your debug logs as they come out

  • Open the last log file created with colour and filters.

However…

  • Not one of these articles is based on Windows… they all mention grep. (being a unix user from waaaay back at least I have heard of grep and understand what piping to grep is). But grep is a unix command, therefore is a mac command. So what about windows.

  • Well if you are using Powershell as your terminal (I think it’s a good idea so you can do more advanced things when needed… it’s similar to a mac user using bash).

  • This article is helpful for doing grep similar in windows

  • So I would do something like this command sfdx force:apex:log:get --logleve DEBUG --color | Select-String -Pattern USER_DEBUG

  • And not one of the articles mentions how to quit log tail when it’s running… Again I had to google that and it’s ctrl+c

  • Oh and all that bit in the article about setting custom colours is mac-based too, so I need to research how to do that on Windows too.

Apex Replay Debugger

  • Docs are here

  • I haven’t played with it yet because it looks kinda hard and I could not work out how to set the checkpoints in VSCode… more to be discovered.