apex Commands

Use the apex commands to create Apex classes, execute anonymous blocks, view your logs, and run and view the results of Apex tests.

apex:class:create

Creates an Apex class in the specified directory or the current working directory. If you don’t explicitly set the API version, it defaults to the current API version. The .cls file and associated metadata file are created.

Command Syntax

sfdx force:apex:class:create
-n CLASSNAME
[-t TEMPLATE]
[-d OUTPUTDIR]
[-a APIVERSION]
[--json]
[--loglevel LOGLEVEL]

Parameters

-n | --classname CLASSNAME
Required




The name of the new Apex class. The name can be up to 40 characters and must start with a letter.

Type: string
-t | --template TEMPLATE
Optional




The template to use to create the file. Supplied parameter values or default values are filled into a copy of the template.

Type: string
Permissible values are: DefaultApexClass, ApexException, ApexUnitTest, InboundEmailService
Default value: DefaultApexClass
-d | --outputdir OUTPUTDIR
Optional




The directory to store the newly created files. The location can be an absolute path or relative to the current working directory. The default is the current directory.

Type: string
-a | --apiversion APIVERSION
Optional




The API version of the created source.

Type: string
Permissible values are: 40.0, 39.0
Default value: 40.0
--json
Optional




Formats output as JSON.

Type: string
--loglevel LOGLEVEL
Optional




The logging level for this command invocation. Logs are stored in $HOME/.sfdx/sfdx.log.

Type: string
Permissible values are: trace, debug, info, warn, error, fatal
Default value: error

Help for apex:class:create

If not supplied, the apiversion, template, and outputdir use default values.

The outputdir can be an absolute path or relative to the current working directory.

Examples:

1$ sfdx force:apex:class:create -n MyClass
1$ sfdx force:apex:class:create -n MyClass -d classes

apex:execute

Executes one or more lines of anonymous Apex code, or executes the code in a local file.

Command Syntax

sfdx force:apex:execute
[-f APEXCODEFILE]
[-u TARGETUSERNAME]
[--json]
[--loglevel LOGLEVEL]

Parameters

-f | --apexcodefile APEXCODEFILE
Optional




Path to a local file that contains Apex code.

Type: file
-u | --targetusername TARGETUSERNAME
Optional




Username for the target org. Overrides the default target org.

Type: string
--json
Optional




Format output as JSON.

Type: flag
--loglevel LOGLEVEL
Optional




The logging level for this command invocation. Logs are stored in $HOME/.sfdx/sfdx.log.

Type: string
Permissible values are: trace, debug, info, warn, error, fatal
Default value: error

Help for apex:execute

Executes one or more lines of Apex code, or executes the code in a local file.

Before you enter code, run this command with no parameters to get a prompt.

From the prompt, all commands are executed in a single execute anonymous request.

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

Examples:

1$ sfdx force:apex:execute -f ~/test.apex
1$ sfdx force:apex:execute
1>> Start typing Apex code. Press the Enter key after each line,
1>> then press CTRL+D when finished.

apex:log:get

Fetches a specific debug log.

Command Syntax

sfdx force:apex:log:get
-i LOGID
[-u TARGETUSERNAME]
[--json]
[--loglevel LOGLEVEL]

Parameters

-i | --logid LOGID
Required




ID of the log to display.

Type: id
-u | --targetusername TARGETUSERNAME
Optional




Username for the target org. Overrides the default target org.

Type: string
--json
Optional




Format output as JSON.

Type: flag
--loglevel LOGLEVEL
Optional




The logging level for this command invocation. Logs are stored in $HOME/.sfdx/sfdx.log.

Type: string
Permissible values are: trace, debug, info, warn, error, fatal
Default value: error

Help for apex:log:get

When you execute this command in a project, it fetches the specified log from your default scratch org.To get the IDs for your debug logs, run "sfdx force:apex:log:list".

Examples:

1$ sfdx force:apex:log:get -i <log id>
1$ sfdx force:apex:log:get -i <log id> -u me@my.org

apex:log:list

Displays a list of debug log IDs, along with general information about the logs.

Command Syntax

sfdx force:apex:log:list
[-u TARGETUSERNAME]
[--json]
[--loglevel LOGLEVEL]

Parameters

-u | --targetusername TARGETUSERNAME
Optional




Username for the target org. Overrides the default target org.

Type: string
--json
Optional




Format output as JSON.

Type: flag
--loglevel LOGLEVEL
Optional




The logging level for this command invocation. Logs are stored in $HOME/.sfdx/sfdx.log.

Type: string
Permissible values are: trace, debug, info, warn, error, fatal
Default value: error

Help for apex:log:list

When you execute this command in a project, it lists the log IDs for your default scratch org.

Examples:

1$ sfdx force:apex:log:list
1$ sfdx force:apex:log:list -u me@my.org

apex:test:report

Displays the test results for a specific test run.

Command Syntax

sfdx force:apex:test:report
-i TESTRUNID
[-c]
[-d OUTPUTDIR]
[-r RESULTFORMAT]
[-w WAIT]
[-u TARGETUSERNAME]
[--json]
[--loglevel LOGLEVEL]
[--verbose]

Parameters

-i | --testrunid TESTRUNID
Required




ID of test run.

Type: id
-c | --codecoverage
Optional




Retrieves code coverage results.

Type: flag
-d | --outputdir OUTPUTDIR
Optional




Directory to store test run files.

Type: directory
-r | --resultformat RESULTFORMAT
Optional




Format to use when displaying test results. If you also specify the --json flag, --json overrides this parameter.

Type: string
Permissible values are: human, tap, junit, json
Default value: human
-w | --wait WAIT
Optional




Sets the streaming client socket timeout, in minutes.If the streaming client socket has no contact from the server for a number of minutes, the client exits. Specify a longer wait time if timeouts occur frequently.

Type: minutes
Default value: 6
-u | --targetusername TARGETUSERNAME
Optional




Username for the target org. Overrides the default target org.

Type: string
--json
Optional




Format output as JSON.

Type: flag
--loglevel LOGLEVEL
Optional




The logging level for this command invocation. Logs are stored in $HOME/.sfdx/sfdx.log.

Type: string
Permissible values are: trace, debug, info, warn, error, fatal
Default value: error
--verbose
Optional




Displays Apex test processing details. If json format is specified, processing details aren’t displayed.

Type: flag

Help for apex:test:report

Displays test results for an enqueued or completed asynchronous Apex test run.

Examples:

1$ sfdx force:apex:test:report -i <test run id>
1$ sfdx force:apex:test:report -i <test run id> -r junit
1$ sfdx force:apex:test:report -i <test run id> -c --json

apex:test:run

Runs Apex tests.

Command Syntax

sfdx force:apex:test:run
[-n CLASSNAMES]
[-s SUITENAMES]
[-c]
[-d OUTPUTDIR]
[-l TESTLEVEL]
[-r RESULTFORMAT]
[-u TARGETUSERNAME]
[--json]
[--loglevel LOGLEVEL]
[--verbose]

Parameters

-n | --classnames CLASSNAMES
Optional




Comma-separated list of Apex test class names to execute. You can’t specify both class names and suite names.

Type: string
-s | --suitenames SUITENAMES
Optional




Comma-separated list of Apex test suite names to execute. You can’t specify both class names and suite names.

Type: string
-c | --codecoverage
Optional




Retrieves code coverage results.

Type: flag
-d | --outputdir OUTPUTDIR
Optional




Directory to store test run files.

Type: directory
-l | --testlevel TESTLEVEL
Optional




Specifies which tests to run, using one of these TestLevel enum values:

RunSpecifiedTests—Only the tests that you specify are run.

RunLocalTests—All tests in your org are run, except the ones that originate from installed managed packages.

RunAllTestsInOrg—All tests are in your org and in installed managed packages are run.

Type: string
Permissible values are: RunLocalTests, RunAllTestsInOrg, RunSpecifiedTests
-r | --resultformat RESULTFORMAT
Optional




Format to use when displaying test results. If you also specify the --json flag, --json overrides this parameter.

Type: string
Permissible values are: human, tap, junit, json
Default value: human
-u | --targetusername TARGETUSERNAME
Optional




Username for the target org. Overrides the default target org.

Type: string
--json
Optional




Format output as JSON.

Type: flag
--loglevel LOGLEVEL
Optional




The logging level for this command invocation. Logs are stored in $HOME/.sfdx/sfdx.log.

Type: string
Permissible values are: trace, debug, info, warn, error, fatal
Default value: error
--verbose
Optional




Displays Apex test processing details. If json format is specified, processing details aren’t displayed.

Type: flag

Help for apex:test:run

By default, runs all Apex tests in the org’s namespace.

To run specific tests, specify the class names or suite names or set a --testlevel value.

Examples:

1$ sfdx force:apex:test:run
1$ sfdx force:apex:test:run -n MyClassTest,MyOtherClassTest -r human
1$ sfdx force:apex:test:run -s MySuite,MyOtherSuite -c --json
1$ sfdx force:apex:test:run -l RunLocalTests -d <path to outputdir> -u me@my.org