Is it possible / How would I run a block of Apex anonymous code from Visual Studio Code?
3 Answers
In the command palette, if you type "sfdx ex" you will be offered this if you have some Apex text selected:
SFDX: Execute Anonymous Apex with Currently Selected Text
or this:
SFDX: Execute Anonymous Apex with Editor Contents
if you don't.
Not too sure why there is also this .apex file thing too. As the project is open source you can see open issues, all issues, pull requests etc here https://github.com/forcedotcom/salesforcedx-vscode/issues.
-
1As far as I know, the .apex file extension is simply to add code highlighting (and perhaps other IDE language features? though no intellisense on my test) for saved scripts--rather than saving them in the project as a .txt file, for instance. Commented Aug 5, 2019 at 22:40
For all those command liners, you can also use force:apex:execute
sfdx force:apex:execute -u MyConnectedOrg
Start typing straight into the terminal or copy/paste your code of interest. Ctrl+D will execute the code
Update Feb 2024
sf
is the new preferred way to use CLI commands, and you can use the following as an equivalent to the example above:
sf apex run -o MyConnectedOrg
See all the options for sf apex run
here
- Create new file (Cmd + N).
- Select
Anonymous Apex
as language for the file.
- SFDX: Execute Anonymous Apex with Editor Contents (I made it as Hot Key (Cmd + Enter))