apex Commands

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

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
[--json]
[--loglevel LOGLEVEL]
-n CLASSNAME
[-t TEMPLATE]
[-d OUTPUTDIR]
[--apiversion APIVERSION]

Parameters

--json
Optional

Formats output as JSON.

Type: boolean
--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, TRACE, DEBUG, INFO, WARN, ERROR, FATAL
Default value: warn
-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
--apiversion APIVERSION
Optional

The API version of the created source.

Warning: the short name for --apiversion is no longer available. If you have any scripts using -a instead of --apiversion, update them to use --apiversion.

Type: string
Permissible values are: 47.0, 46.0
Default value: 47.0

Help for apex:class:create

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
[--json]
[--loglevel LOGLEVEL]
[-u TARGETUSERNAME]
[--apiversion APIVERSION]
[-f APEXCODEFILE]

Parameters

--json
Optional

Format output as JSON.

Type: boolean
--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, TRACE, DEBUG, INFO, WARN, ERROR, FATAL
Default value: warn
-u | --targetusername TARGETUSERNAME
Optional

A username or alias for the target org. Overrides the default target org.

Type: string
--apiversion APIVERSION
Optional

Override the API version used for API requests made by this command.

Type: string
-f | --apexcodefile APEXCODEFILE
Optional

Path to a local file that contains Apex code.

Type: filepath

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 the last debug log.

Command Syntax

sfdx force:apex:log:get
[--json]
[--loglevel LOGLEVEL]
[-u TARGETUSERNAME]
[--apiversion APIVERSION]
[-c]
[-i LOGID]
[-n NUMBER]

Parameters

--json
Optional

Format output as JSON.

Type: boolean
--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, TRACE, DEBUG, INFO, WARN, ERROR, FATAL
Default value: warn
-u | --targetusername TARGETUSERNAME
Optional

A username or alias for the target org. Overrides the default target org.

Type: string
--apiversion APIVERSION
Optional

Override the API version used for API requests made by this command.

Type: string
-c | --color
Optional

Applies default colors to noteworthy log lines.

Type: boolean
-i | --logid LOGID
Optional

ID of the log to display.

Type: id
-n | --number NUMBER
Optional

Number of most recent logs to display.

Type: number

Help for apex:log:get

When you execute this command in a project, it fetches the specified log or given number of last logs from your default scratch org.

To get the IDs for your debug logs, run "sfdx force:apex:log:list".

To specify a count of logs to return, use the --number parameter to return the most recent logs.

Executing this command without parameters returns the most recent log.

Examples:

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

apex:log:list

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

Command Syntax

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

Parameters

--json
Optional

Format output as JSON.

Type: boolean
--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, TRACE, DEBUG, INFO, WARN, ERROR, FATAL
Default value: warn
-u | --targetusername TARGETUSERNAME
Optional

A username or alias for the target org. Overrides the default target org.

Type: string
--apiversion APIVERSION
Optional

Override the API version used for API requests made by this command.

Type: string

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:log:tail

Activates debug logging and displays logs in the terminal. You can also pipe the logs to a file.

Command Syntax

sfdx force:apex:log:tail
[--json]
[--loglevel LOGLEVEL]
[-u TARGETUSERNAME]
[--apiversion APIVERSION]
[-c]
[-d DEBUGLEVEL]
[-s]

Parameters

--json
Optional

Format output as JSON.

Type: boolean
--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, TRACE, DEBUG, INFO, WARN, ERROR, FATAL
Default value: warn
-u | --targetusername TARGETUSERNAME
Optional

A username or alias for the target org. Overrides the default target org.

Type: string
--apiversion APIVERSION
Optional

Override the API version used for API requests made by this command.

Type: string
-c | --color
Optional

Applies default colors to noteworthy log lines.

Type: boolean
-d | --debuglevel DEBUGLEVEL
Optional

Debug level to set on the DEVELOPER_LOG trace flag for your user.

Type: string
-s | --skiptraceflag
Optional

Skips trace flag setup. Assumes that a trace flag and debug level are fully set up.

Type: boolean

Help for apex:log:tail

Tails logs from your target org for 30 minutes.

If a DEVELOPER_LOG trace flag does not exist, this command creates one in the target org.

If the active trace flag's expiration date is within this command's timeout window, the command sets the trace flag's expiration date to 30 minutes from the current time.

The --debuglevel parameter assigns a debug level to the active DEVELOPER_LOG trace flag.

Use --skiptraceflag to skip trace flag setup, including setting expiration date and debug level. Include this flag only if there is an active user-based trace flag for your user.

The --json parameter emits log lines in JSON, but does not follow the standard Salesforce CLI JSON format (which includes status and result values).

Examples:

1$ sfdx force:apex:log:tail
1$ sfdx force:apex:log:tail --debuglevel MyDebugLevel
1$ sfdx force:apex:log:tail -c -s

apex:test:report

Displays the test results for a specific test run.

Command Syntax

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

Parameters

--json
Optional

Format output as JSON.

Type: boolean
--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, TRACE, DEBUG, INFO, WARN, ERROR, FATAL
Default value: warn
-u | --targetusername TARGETUSERNAME
Optional

A username or alias for the target org. Overrides the default target org.

Type: string
--apiversion APIVERSION
Optional

Override the API version used for API requests made by this command.

Type: string
-i | --testrunid TESTRUNID
Required

The ID of test run.

Type: id
-c | --codecoverage
Optional

Retrieves code coverage results.

Type: boolean
-d | --outputdir OUTPUTDIR
Optional

Directory to store test run files.

Type: directory
-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
--verbose
Optional

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

Type: boolean
-r | --resultformat RESULTFORMAT
Optional

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

Type: string
Permissible values are: human, tap, junit, json
Default value: human

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
[--json]
[--loglevel LOGLEVEL]
[-u TARGETUSERNAME]
[--apiversion APIVERSION]
[-n CLASSNAMES]
[-s SUITENAMES]
[-t TESTS]
[-c]
[-d OUTPUTDIR]
[-l TESTLEVEL]
[-w WAIT]
[-y]
[--verbose]
[-r RESULTFORMAT]

Parameters

--json
Optional

Format output as JSON.

Type: boolean
--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, TRACE, DEBUG, INFO, WARN, ERROR, FATAL
Default value: warn
-u | --targetusername TARGETUSERNAME
Optional

A username or alias for the target org. Overrides the default target org.

Type: string
--apiversion APIVERSION
Optional

Override the API version used for API requests made by this command.

Type: string
-n | --classnames CLASSNAMES
Optional

Comma-separated list of Apex test class names to run. You can specify only one of classnames, suitenames, or tests.

Type: array
-s | --suitenames SUITENAMES
Optional

Comma-separated list of Apex test suite names to run. You can only specify one of classnames, suitenames, or tests.

Type: array
-t | --tests TESTS
Optional

Comma-separated list of Apex test class names or IDs and test methods, if applicable, to run. You can only specify one of classnames, suitenames, or tests.

Type: array
-c | --codecoverage
Optional

Retrieves code coverage results.

Type: boolean
-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
-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
-y | --synchronous
Optional

Runs test methods from a single Apex class synchronously. If you don't specify this flag, tests are run asynchronously.

Type: boolean
--verbose
Optional

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

Type: boolean
-r | --resultformat RESULTFORMAT
Optional

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

Type: string
Permissible values are: human, tap, junit, json

Help for apex:test:run

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

To run specific test classes, specify class names or suite names, or set a --testlevel value.

To run specific test methods, use --tests.

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 -t MyClassTest.testCoolFeature,MyClassTest.testAwesomeFeature,AnotherClassTest,namespace.TheirClassTest.testThis -r human
1$ sfdx force:apex:test:run -l RunLocalTests -d <path to outputdir> -u me@my.org

apex:trigger:create

Creates an Apex trigger 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 .trigger file and associated metadata file are created.

Command Syntax

sfdx force:apex:trigger:create
[--json]
[--loglevel LOGLEVEL]
-n TRIGGERNAME
[-t TEMPLATE]
[-d OUTPUTDIR]
[--apiversion APIVERSION]
[-s SOBJECT]
[-e TRIGGEREVENTS]

Parameters

--json
Optional

Formats output as JSON.

Type: boolean
--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, TRACE, DEBUG, INFO, WARN, ERROR, FATAL
Default value: warn
-n | --triggername TRIGGERNAME
Required

The name of the new Apex trigger. 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: ApexTrigger
Default value: ApexTrigger
-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
--apiversion APIVERSION
Optional

The API version of the created source.

Warning: the short name for --apiversion is no longer available. If you have any scripts using -a instead of --apiversion, update them to use --apiversion.

Type: string
Permissible values are: 47.0, 46.0
Default value: 47.0
-s | --sobject SOBJECT
Optional

The sObject to create an Apex trigger on.

Type: string
Default value: SOBJECT
-e | --triggerevents TRIGGEREVENTS
Optional

The events that cause the trigger to fire.

Type: array
Default value: before insert

Help for apex:trigger:create