9

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.

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

4 Answers 4

6

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.

1
  • 1
    That was the problem.
    – Stefano
    Commented Jul 31, 2020 at 15:28
8

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
6
  • 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
    Commented Jul 31, 2020 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
    Commented Jul 31, 2020 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
    Commented Jul 31, 2020 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
    Commented Jul 31, 2020 at 9:57
  • 1
    I solved the issue adding an User Trace Flag from Setup -> Debug Logs.
    – Stefano
    Commented Jul 31, 2020 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.

0

You can see the result in the output tab.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .