apex Commands

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

force: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.

Help for force: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

Command Syntax

sfdx force:apex:class:create
[--json]
[--loglevel LOGLEVEL]
-n CLASSNAME
[-t TEMPLATE]
[-d OUTPUTDIR]
[--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: enum
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: ApexException, ApexUnitTest, DefaultApexClass, 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
Default value: .
--apiversion APIVERSION
Optional

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

Type: string

force:apex:execute

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

Help for force:apex:execute

If you don’t run this command from within a Salesforce DX project, —-targetusername is required.

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

Examples for force:apex:execute

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

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: enum
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

force:apex:log:get

Fetches the specified log or given number of most recent logs from the scratch org. .

Help for force:apex:log:get

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

Use the --logid parameter to return a specific log.

Use the --number parameter to return the specified number of recent logs.

Use the --outputdir parameter to specify the directory to store the logs in.

Executing this command without parameters returns the most recent log.

Examples for force:apex:log:get

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
1$ sfdx force:apex:log:get -d Users/Desktop/logs -n 2

Command Syntax

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

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: enum
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 | --logid LOGID
Optional

Id of the log to display.

Type: id
-n | --number NUMBER
Optional

Number of most recent logs to display.

Type: number
-d | --outputdir OUTPUTDIR
Optional

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

Type: string

force:apex:log:list

Run this command in a project to list the IDs and general information for all debug logs in your default org.

Help for force:apex:log:list

To fetch a specific log from your org, obtain the ID from this command's output, then run the “sfdx force:apex:log:get” command.

Examples for force:apex:log:list

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

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: enum
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

force:apex:log:tail

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

Help for force: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

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: enum
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

force:apex:test:report

Displays the test results for a specific test run.

Help for force: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

Command Syntax

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

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: enum
Permissible values are: trace, debug, info, warn, error, fatal, TRACE, DEBUG, INFO, WARN, ERROR, FATAL
Default value: warn
-r | --resultformat RESULTFORMAT
Optional

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

Type: enum
Permissible values are: human, tap, junit, json
Default value: human
-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 minutes
--verbose
Optional

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

Type: boolean

force:apex:test:run

Runs Apex tests.

Help for force: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

Command Syntax

sfdx force:apex:test:run
[--json]
[--loglevel LOGLEVEL]
[-r RESULTFORMAT]
[-u TARGETUSERNAME]
[--apiversion APIVERSION]
[-n CLASSNAMES]
[-s SUITENAMES]
[-t TESTS]
[-c]
[-d OUTPUTDIR]
[-l TESTLEVEL]
[-w WAIT]
[-y]
[--verbose]

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: enum
Permissible values are: trace, debug, info, warn, error, fatal, TRACE, DEBUG, INFO, WARN, ERROR, FATAL
Default value: warn
-r | --resultformat RESULTFORMAT
Optional

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

Type: enum
Permissible values are: human, tap, junit, json
-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: enum
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

force: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.

Help for force:apex:trigger: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:trigger:create -n MyTrigger
1$ sfdx force:apex:trigger:create -n MyTrigger -s Account -e 'before insert,after insert'
1$ sfdx force:apex:trigger:create -n MyTrigger -d triggers

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

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: enum
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
Default value: .
--apiversion APIVERSION
Optional

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

Type: string
-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