apex run

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

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.

force:apex:execute

Flag Name (Long)Flag Name (Short)Description
‑‑api‑versionN/AType: Value

Override the api version used for api requests made by this command
‑‑category‑levelN/AType: Value

Log level for a specific log category in the debug log generated in the org. Use the format: Category=Level.

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.

Valid categories: Db, Workflow, Validation, Callout, Apex_code, Apex_profiling, Visualforce, System, Wave, Nba, All.

Valid levels: NONE, ERROR, WARN, INFO, DEBUG, FINE, FINER, FINEST.

Can be specified multiple times. Mutually exclusive with –debug-level.
‑‑debug‑level‑dType: Value
Valid Values: NONE, DEBUGONLY, DB, PROFILING, CALLOUT, DETAIL

Debug level to use for the debug log that’s generated in the org.

Sets the debug log level for the anonymous Apex execution. Defaults to DEBUGONLY if not specified. Mutually exclusive with –category-level.
‑‑file‑fType: Value

Path to a local file that contains Apex code.
‑‑flags‑dirN/AType: Value

Import flag values from a directory.
‑‑jsonN/AType: Boolean

Format output as json.
‑‑target‑org‑oType: Value
Required

Username or alias of the target org. Not required if the target-org configuration variable is already set.

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

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

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.

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

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