Newer Version Available

This content describes an older version of this product. View Latest

Execute Anonymous Apex

You can execute an anonymous block of Apex code in an org with the apex run Salesforce CLI command.
  1. Open a terminal (macOS and Linux) or command prompt Windows and change to your Salesforce DX project directory.
  2. Run the apex run command with no flags to open an interactive shell. At the prompt, enter all your Apex code; press CTRL-D when you're finished. Your code is then executed in a single execute anonymous request in the specified org, or the default org if you don’t specify one.
    This output shows an example of executing the Apex code system.debug ('Hello world!');

    Use the --file flag to execute Apex code in a file rather than interactively.