How to run apex code form VS Code using sfdx cli

Using sf apex run salesforce cli command to run piece of apex code from CLI of vs code and also run any apex file in your project.

Execute anonymous Apex code from vs code

we can run apex code from vs code terminal using sf apex run command.
  • First we need to enter sf apex run command
  • Then it will wait for enter code, you can write more then one line using enter.
  • Once you enter all your code, then press CRL+D. 
  • Then it will run the code and show the log in terminal.
sf apex run
// Enter your code or paste copied code, Press the Enter key after each line, then press CTRL+D when finished.
system.debug('hello world');
Run-apex-cli

Run Apex code from local file

Execute the Apex code that's in the apex file.
  • First, create a sample apex file in any location.
  • Open that file, and add some apex code to execute.
  • then run the command.
sf apex run -f scripts/apex/hello.apex
Run-apex-file

Flags:

  • -f : apex file with path

Source:
https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_apex_commands_unified.htm#cli_reference_apex_run_unified

Labels:
Join the conversation