3

I installed the Salesforce CLI and the Salesforce Salesforce Extension Pack on Visual Studio Code because it's easier to write code than the Developer Console but I got a problem. I want to get the debug log files when I run some code from Anonymous Block, but I can only see the output in the 'OUTPUT' tab on VSCode, there's no file in the project folder. How can I fix this?

Example (anonymous code from VSCode):

Account a = new Account();
a.name = 'Test Account';
insert a;
System.debug('Account inserted');

How can see the log file of the execution of this code block?

Solution: I solved the issue adding an User Trace Flag from Setup -> Debug Logs.

  • Have you enabled debug logging in setup screen in salesforce for the user? – javanoob Jul 31 at 14:21
  • Thank you ! I solved the issue adding an User Trace Flag from Setup -> Debug Logs. – Stefano Jul 31 at 15:02
1

I would make sure you have an active User Trace Flag. I have noticed that in vscode sometimes you need to have your user trace flag set even when running anonymous apex.

| improve this answer | |
  • 1
    That was the problem. – Stefano Jul 31 at 15:28
3

You can see the logs by

  1. Opening the Command Palette and enter sfdx get in the search box
  2. Then choose SFDX: Get Apex Debug Logs... After a few seconds, you are prompted to select a debug log to download.

Link

Logs

or you can use sfdx command to get the details after setting up the logging.

sfdx force:apex:log:list

The above command will get you a list and then you can use below command to get particular log.

sfdx force:apex:log:get --logid 07L9A000000aBYGUA2
| improve this answer | |
  • I already tried but I got only old logs (from yesterday), why? I also tried to retrieve data clicking 'Retrieve Source in Manifest from Org' but it didn't solve the problem. – Stefano Jul 31 at 9:49
  • Can you open the developer console and then close it, that will make sure logging is set up and we can at least rule out the logging setup issues. Then try and execute something in VS CODE and fetch the logs. Also make sure that the org is correct in VS code – manjit5190 Jul 31 at 9:49
  • Anonymous code executed from Developer Console in the browser and fetching logs from VSCode works, but if I try to execute anonymouse code directly from VSCode seems like no debug log is generated, so I can't fetch it. – Stefano Jul 31 at 9:54
  • 1
    Then I think the VS Code is not setting up the logging properly. Can you check what command is executed when you execute a anonymous block of code in VS Code. It should have be sfdx force:apex:execute -f "YOU FILE PATH" --loglevel "TRACE or something" – manjit5190 Jul 31 at 9:57
  • 1
    I solved the issue adding an User Trace Flag from Setup -> Debug Logs. – Stefano Jul 31 at 15:03
0

What I usually do is periodically (because it is short lived) run the command to turn on Apex replay debugging in VSCode, then run the script.

You'll then be able to run the sfdx command to get debug logs. This will return a list of the logs you would ordinarily see in the Dev Console.

You can set breakpoints if you want.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.