apex run

<!-- prettier-ignore-start -->

Execute anonymous Apex code entered on the command line or from a local file.

Description for apex run

If you don’t run this command from within a Salesforce DX project, you must specify the —-target-org flag.

To execute your code interactively, run this command with no flags. 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.

For more information, see "Anonymous Blocks" in the Apex Developer Guide.

Aliases for apex run

force:apex:execute

Flags

Flag Name (Long) Flag Name (Short) Description
<code>‑‑api‑version</code> N/A Type: Value<br><br>Override the api version used for api requests made by this command
<code>‑‑category‑level</code> N/A Type: Value<br><br>Log level for a specific log category in the debug log generated in the org. Use the format: Category=Level.<br><br>Use this flag to set individual log category levels for fine-grained control over the debug log. Use this format: Category=Level, such as Apex_code=FINEST.<br><br>Valid categories: Db, Workflow, Validation, Callout, Apex_code, Apex_profiling, Visualforce, System, Wave, Nba, All.<br><br>Valid levels: NONE, ERROR, WARN, INFO, DEBUG, FINE, FINER, FINEST.<br><br>Can be specified multiple times. Mutually exclusive with --debug-level.
<code>‑‑debug‑level</code> <code>‑d</code> Type: Value<br>Valid Values: NONE, DEBUGONLY, DB, PROFILING, CALLOUT, DETAIL<br><br>Debug level to use for the debug log that's generated in the org.<br><br>Sets the debug log level for the anonymous Apex execution. Defaults to DEBUGONLY if not specified. Mutually exclusive with --category-level.
<code>‑‑file</code> <code>‑f</code> Type: Value<br><br>Path to a local file that contains Apex code.
<code>‑‑flags‑dir</code> N/A Type: Value<br><br>Import flag values from a directory.
<code>‑‑json</code> N/A Type: Boolean<br><br>Format output as json.
<code>‑‑target‑org</code> <code>‑o</code> Type: Value<br>Required<br><br>Username or alias of the target org. Not required if the target-org configuration variable is already set.

Examples for apex run

Execute the Apex code that's in the ~/test.apex file in the org with the specified username:

sf apex run --target-org testusername@salesforce.org --file ~/test.apex

Similar to previous example, but execute the code in your default org:

sf apex run --file ~/test.apex

Run the command with no flags to start interactive mode; the code will execute in your default org when you exit. At the prompt, start type Apex code and press the Enter key after each line. Press CTRL+D when finished.

sf apex run

Execute the Apex code and generate a debug log with maximum detail:

sf apex run --file ~/test.apex --debug-level DETAIL

Execute the Apex code and generate a debug log with fine-grained control over specific log categories:

sf apex run --file ~/test.apex --category-level Apex_code=FINEST --category-level Db=FINE
<!-- prettier-ignore-end -->