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.

As of April 20, 2023, we no longer maintain this section of the command reference. Refer to the reference information about the sf-style commands instead, which we update regularly. We keep this reference information about the sfdx-style commands as-is for historical reference only.

Don't worry, this command and its flags continue to work the same as before, and any scripts that use the command won’t break. However, we recommend that you start using the equivalent sf-style apex generate class command instead. Here’s how the flags changed between the old and new commands; if a flag isn't listed, the old and new names are the same:

  • Removed flag: --loglevel. Use the SF_LOG_LEVEL environment variable instead.
  • Changed flag name: Old name --apiversion. New name: --api-version.
  • Changed flag name: Old name --outputdir. New name: --output-dir.
  • Changed flag name: Old name --classname. New name: --name.

For background information about this change, read this blog post, which describes how we've updated many of the existing sfdx commands to use the improvements we made in sf. We improved this particular command on February 16, 2023.

Warning

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:

1sfdx force:apex:class:create -n MyClass
1sfdx 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/.sf/sf.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.

As of April 20, 2023, we no longer maintain this section of the command reference. Refer to the reference information about the sf-style commands instead, which we update regularly. We keep this reference information about the sfdx-style commands as-is for historical reference only.

Don't worry, this command and its flags continue to work the same as before, and any scripts that use the command won’t break. However, we recommend that you start using the equivalent sf-style apex run command instead. Here’s how the flags changed between the old and new commands; if a flag isn't listed, the old and new names are the same:

  • Removed flag: --loglevel. Use the SF_LOG_LEVEL environment variable instead.
  • Changed flag name: Old name --apiversion. New name: --api-version.
  • Changed flag name: Old name --targetusername. New name: --target-org, with new short name -o.
  • Changed flag name: Old name --apexcodefile. New name: --file.

For background information about this change, read this blog post, which describes how we've updated many of the existing sfdx commands to use the improvements we made in sf. We improved this particular command on March 2, 2023.

Warning

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

1sfdx force:apex:execute -u testusername@salesforce.org -f ~/test.apex
1sfdx force:apex:execute -f ~/test.apex
1sfdx 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. .

As of April 20, 2023, we no longer maintain this section of the command reference. Refer to the reference information about the sf-style commands instead, which we update regularly. We keep this reference information about the sfdx-style commands as-is for historical reference only.

Don't worry, this command and its flags continue to work the same as before, and any scripts that use the command won’t break. However, we recommend that you start using the equivalent sf-style apex get log command instead. Here’s how the flags changed between the old and new commands; if a flag isn't listed, the old and new names are the same:

  • Removed flag: --loglevel. Use the SF_LOG_LEVEL environment variable instead.
  • Changed flag name: Old name --apiversion. New name: --api-version.
  • Changed flag name: Old name --targetusername. New name: --target-org, with new short name -o.
  • Changed flag name: Old name --outputdir. New name: --output-dir.
  • Changed flag name: Old name --logid. New name: --log-id.

For background information about this change, read this blog post, which describes how we've updated many of the existing sfdx commands to use the improvements we made in sf. We improved this particular command on March 2, 2023.

Warning

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

1sfdx force:apex:log:get -i <log id>
1sfdx force:apex:log:get -i <log id> -u me@my.org
1sfdx force:apex:log:get -n 2 -c
1sfdx 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.

As of April 20, 2023, we no longer maintain this section of the command reference. Refer to the reference information about the sf-style commands instead, which we update regularly. We keep this reference information about the sfdx-style commands as-is for historical reference only.

Don't worry, this command and its flags continue to work the same as before, and any scripts that use the command won’t break. However, we recommend that you start using the equivalent sf-style apex list log command instead. Here’s how the flags changed between the old and new commands; if a flag isn't listed, the old and new names are the same:

  • Removed flag: --loglevel. Use the SF_LOG_LEVEL environment variable instead.
  • Changed flag name: Old name --apiversion. New name: --api-version.
  • Changed flag name: Old name --targetusername. New name: --target-org, with new short name -o.

For background information about this change, read this blog post, which describes how we've updated many of the existing sfdx commands to use the improvements we made in sf. We improved this particular command on March 2, 2023.

Warning

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

1sfdx force:apex:log:list
1sfdx 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.

As of April 20, 2023, we no longer maintain this section of the command reference. Refer to the reference information about the sf-style commands instead, which we update regularly. We keep this reference information about the sfdx-style commands as-is for historical reference only.

Don't worry, this command and its flags continue to work the same as before, and any scripts that use the command won’t break. However, we recommend that you start using the equivalent sf-style apex tail log command instead. Here’s how the flags changed between the old and new commands; if a flag isn't listed, the old and new names are the same:

  • Removed flag: --loglevel. Use the SF_LOG_LEVEL environment variable instead.
  • Changed flag name: Old name --apiversion. New name: --api-version.
  • Changed flag name: Old name --targetusername. New name: --target-org, with new short name -o.
  • Changed flag name: Old name --debuglevel. New name: --debug-level.
  • Changed flag name: Old name --skiptraceflag. New name: --skip-trace-flag.

For background information about this change, read this blog post, which describes how we've updated many of the existing sfdx commands to use the improvements we made in sf. We improved this particular command on March 2, 2023.

Warning

Examples for force:apex:log:tail

1sfdx force:apex:log:tail
1sfdx force:apex:log:tail --debuglevel MyDebugLevel
1sfdx 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

Provide a test run ID to display test results for an enqueued or completed asynchronous test run. The test run ID is displayed after running the "sfdx force:apex:test:run" command.

As of April 20, 2023, we no longer maintain this section of the command reference. Refer to the reference information about the sf-style commands instead, which we update regularly. We keep this reference information about the sfdx-style commands as-is for historical reference only.

Don't worry, this command and its flags continue to work the same as before, and any scripts that use the command won’t break. However, we recommend that you start using the equivalent sf-style apex get test command instead. Here’s how the flags changed between the old and new commands; if a flag isn't listed, the old and new names are the same:

  • Removed flag: --loglevel. Use the SF_LOG_LEVEL environment variable instead.
  • Changed flag name: Old name --apiversion. New name: --api-version.
  • Changed flag name: Old name --targetusername. New name: --target-org, with new short name -o.
  • Changed flag name: Old name --outputdir. New name: --output-dir.
  • Changed flag name: Old name --codecoverage. New name: --code-coverage.
  • Changed flag name: Old name --testrunid. New name: --test-run-id.
  • Changed flag name: Old name --resultformat. New name: --result-format.

For background information about this change, read this blog post, which describes how we've updated many of the existing sfdx commands to use the improvements we made in sf. We improved this particular command on March 2, 2023.

Warning

Examples for force:apex:test:report

1sfdx force:apex:test:report -i <test run id>
1sfdx force:apex:test:report -i <test run id> -r junit
1sfdx force:apex:test:report -i <test run id> -c --json
1sfdx force:apex:test:report -i <test run id> -c -d <path to outputdir> -u me@myorg

Command Syntax

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

Parameters

--json
Optional

Format output as JSON.

Type: boolean
--loglevel LOGLEVEL
Optional

Permissible values are: trace, debug, info, warn, error, fatal, TRACE, DEBUG, INFO, WARN, ERROR, FATAL

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 | --testrunid TESTRUNID
Required

The ID of the test run.

Type: string
-c | --codecoverage
Optional

Retrieves code coverage results.

Type: boolean
-d | --outputdir OUTPUTDIR
Optional

Directory to store test result files.

Type: string
-r | --resultformat RESULTFORMAT
Optional

Permissible values are: human, tap, junit, json.

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

Sets the streaming client socket timeout in minutes; specify a longer wait time if timeouts occur frequently.

Type: string
--verbose
Optional

Emit additional command output to stdout.

Type: boolean

force:apex:test:run

Specify which tests to run by using the --classnames, --suites, or --tests parameters. Alternatively, use the --testlevel parameter to run all the tests in your org, local tests, or specified tests.

As of April 20, 2023, we no longer maintain this section of the command reference. Refer to the reference information about the sf-style commands instead, which we update regularly. We keep this reference information about the sfdx-style commands as-is for historical reference only.

Don't worry, this command and its flags continue to work the same as before, and any scripts that use the command won’t break. However, we recommend that you start using the equivalent sf-style apex run test command instead. Here’s how the flags changed between the old and new commands; if a flag isn't listed, the old and new names are the same:

  • Removed flag: --loglevel. Use the SF_LOG_LEVEL environment variable instead.
  • Changed flag name: Old name --apiversion. New name: --api-version.
  • Changed flag name: Old name --targetusername. New name: --target-org, with new short name -o.
  • Changed flag name: Old name --codecoverage. New name: --code-coverage.
  • Changed flag name: Old name --outputdir. New name: --output-dir.
  • Changed flag name: Old name --apexcodefile. New name: --file.
  • Changed flag name: Old name --testlevel. New name: --test-level.
  • Changed flag name: Old name --classnames. New name: --class-names.
  • Changed flag name: Old name --resultformat. New name: --result-format.
  • Changed flag name: Old name --suitenames. New name: --suite-names.
  • Changed flag name: Old name --detailedcoverage. New name: --detailed-coverage.

For background information about this change, read this blog post, which describes how we've updated many of the existing sfdx commands to use the improvements we made in sf. We improved this particular command on March 2, 2023.

Warning

Help for force:apex:test:run

To see code coverage results, use the --codecoverage parameter with --resultformat. The output displays a high-level summary of the test run and the code coverage values for classes in your org. If you specify human-readable result format, use the --detailedcoverage parameter to see detailed coverage results for each test method run.

NOTE: The testRunCoverage value (JSON and JUnit result formats) is a percentage of the covered lines and total lines from all the Apex classes evaluated by the tests in this run.

Examples for force:apex:test:run

1sfdx force:apex:test:run
1sfdx force:apex:test:run -n "MyClassTest,MyOtherClassTest" -r human
1sfdx force:apex:test:run -s "MySuite,MyOtherSuite" -c -v --json
1sfdx force:apex:test:run -t "MyClassTest.testCoolFeature,MyClassTest.testAwesomeFeature,AnotherClassTest,namespace.TheirClassTest.testThis" -r human
1sfdx force:apex:test:run -l RunLocalTests -d <path to outputdir> -u me@my.org

Command Syntax

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

Parameters

--json
Optional

Format output as JSON.

Type: boolean
--loglevel LOGLEVEL
Optional

Permissible values are: trace, debug, info, warn, error, fatal, TRACE, DEBUG, INFO, WARN, ERROR, FATAL

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 | --codecoverage
Optional

Retrieves code coverage results.

Type: boolean
-d | --outputdir OUTPUTDIR
Optional

Directory to store test run files.

Type: string
-l | --testlevel TESTLEVEL
Optional

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

Type: enum
Permissible values are: RunLocalTests, RunAllTestsInOrg, RunSpecifiedTests
-n | --classnames CLASSNAMES
Optional

Comma-separated list of Apex test class names to run; if you select --classnames, you can't specify --suitenames or --tests.

Type: string
-r | --resultformat RESULTFORMAT
Optional

Permissible values are: human, tap, junit, json.

Type: enum
Permissible values are: human, tap, junit, json
-s | --suitenames SUITENAMES
Optional

Comma-separated list of Apex test suite names to run; if you select --suitenames, you can't specify --classnames or --tests.

Type: string
-t | --tests TESTS
Optional

Comma-separated list of Apex test class names or IDs and, if applicable, test methods to run; if you specify --tests, you can't specify --classnames or --suitenames.

Type: string
-w | --wait WAIT
Optional

Sets the streaming client socket timeout in minutes; specify a longer wait time if timeouts occur frequently.

Type: string
-y | --synchronous
Optional

Runs test methods from a single Apex class synchronously; if not specified, tests are run ansynchronously.

Type: boolean
--verbose
Optional

Emit additional command output to stdout.

Type: boolean
-v | --detailedcoverage
Optional

Display detailed code coverage per test.

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.

As of April 20, 2023, we no longer maintain this section of the command reference. Refer to the reference information about the sf-style commands instead, which we update regularly. We keep this reference information about the sfdx-style commands as-is for historical reference only.

Don't worry, this command and its flags continue to work the same as before, and any scripts that use the command won’t break. However, we recommend that you start using the equivalent sf-style apex generate trigger command instead. Here’s how the flags changed between the old and new commands; if a flag isn't listed, the old and new names are the same:

  • Removed flag: --loglevel. Use the SF_LOG_LEVEL environment variable instead.
  • Changed flag name: Old name --apiversion. New name: --api-version.
  • Changed flag name: Old name --outputdir. New name: --output-dir.
  • Changed flag name: Old name --triggername. New name: --name.
  • Changed flag name: Old name --triggerevents. New name: --event.

For background information about this change, read this blog post, which describes how we've updated many of the existing sfdx commands to use the improvements we made in sf. We improved this particular command on February 16, 2023.

Warning

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:

1sfdx force:apex:trigger:create -n MyTrigger
1sfdx force:apex:trigger:create -n MyTrigger -s Account -e 'before insert,after insert'
1sfdx 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/.sf/sf.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