apex Commands
apex:class:create
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: 44.0, 43.0
- Default value: 44.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 MyClass1$ sfdx force:apex:class:create -n MyClass -d classesapex:execute
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: filepath
- -u | --targetusername TARGETUSERNAME
- Optional
-
A username or alias 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.apex1$ sfdx force:apex:execute1>> Start typing Apex code. Press the Enter key after each line,1>> then press CTRL+D when finished.apex:log:get
Command Syntax
- sfdx force:apex:log:get
- [-c]
- [-i LOGID]
- [-n NUMBER]
- [-u TARGETUSERNAME]
- [--json]
- [--loglevel LOGLEVEL]
Parameters
- -c | --color
- Optional
-
Applies default colors to noteworthy log lines.
- Type: flag
- -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
- -u | --targetusername TARGETUSERNAME
- Optional
-
A username or alias 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 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.org1$ sfdx force:apex:log:get -n 2 -capex:log:list
Command Syntax
- sfdx force:apex:log:list
- [-u TARGETUSERNAME]
- [--json]
- [--loglevel LOGLEVEL]
Parameters
- -u | --targetusername TARGETUSERNAME
- Optional
-
A username or alias 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:list1$ sfdx force:apex:log:list -u me@my.orgapex:log:tail
Command Syntax
- sfdx force:apex:log:tail
- [-c]
- [-d DEBUGLEVEL]
- [-s]
- [-u TARGETUSERNAME]
- [--json]
- [--loglevel LOGLEVEL]
Parameters
- -c | --color
- Optional
-
Applies default colors to noteworthy log lines.
- Type: flag
- -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: flag
- -u | --targetusername TARGETUSERNAME
- Optional
-
A username or alias 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: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:tail1$ sfdx force:apex:log:tail --debuglevel MyDebugLevel1$ sfdx force:apex:log:tail -c -sapex:test:report
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
-
The 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
-
A username or alias 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 junit1$ sfdx force:apex:test:report -i <test run id> -c --jsonapex:test:run
Command Syntax
- sfdx force:apex:test:run
- [-n CLASSNAMES]
- [-s SUITENAMES]
- [-t TESTS]
- [-c]
- [-d OUTPUTDIR]
- [-l TESTLEVEL]
- [-r RESULTFORMAT]
- [-w WAIT]
- [-y]
- [-u TARGETUSERNAME]
- [--json]
- [--loglevel LOGLEVEL]
- [--verbose]
Parameters
- -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: string
- -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: string
- -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: 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
- -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
- -y | --synchronous
- Optional
-
Runs test methods from a single Apex class synchronously. If you don't specify this flag, tests are run asynchronously.
- Type: flag
- -u | --targetusername TARGETUSERNAME
- Optional
-
A username or alias 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 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:run1$ sfdx force:apex:test:run -n MyClassTest,MyOtherClassTest -r human1$ sfdx force:apex:test:run -s MySuite,MyOtherSuite -c --json1$ sfdx force:apex:test:run -t MyClassTest.testCoolFeature,MyClassTest.testAwesomeFeature,AnotherClassTest,namespace.TheirClassTest.testThis -r human1$ sfdx force:apex:test:run -l RunLocalTests -d <path to outputdir> -u me@my.orgapex:trigger:create
Command Syntax
- sfdx force:apex:trigger:create
- -n TRIGGERNAME
- [-t TEMPLATE]
- [-d OUTPUTDIR]
- [-a APIVERSION]
- [-s SOBJECT]
- [-e TRIGGEREVENTS]
- [--json]
- [--loglevel LOGLEVEL]
Parameters
- -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
- -a | --apiversion APIVERSION
- Optional
-
The API version of the created source.
- Type: string
- Permissible values are: 44.0, 43.0
- Default value: 44.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: string
- Permissible values are: before insert, before update, before delete, after insert, after update, after delete, after undelete
- Default value: before insert
- --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: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 MyTrigger1$ sfdx force:apex:trigger:create -n MyTrigger -s Account -e 'before insert, after upsert'1$ sfdx force:apex:trigger:create -n MyTrigger -d triggers