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
‑‑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.