apex Commands
force:apex:class:create
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 MyClass1$ sfdx force:apex:class:create -n MyClass -d classesCommand 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
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.apex1$ sfdx force:apex:execute -f ~/test.apex1$ 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
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.org1$ sfdx force:apex:log:get -n 2 -c1$ sfdx force:apex:log:get -d Users/Desktop/logs -n 2Command 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
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:list1$ sfdx force:apex:log:list -u me@my.orgCommand 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
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:tail1$ sfdx force:apex:log:tail --debuglevel MyDebugLevel1$ sfdx force:apex:log:tail -c -sCommand 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
Examples for force:apex:test:report
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 --json1$ sfdx force:apex:test:report -i <test run id> -c -d <path to outputdir> -u me@myorgCommand 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
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
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 -v --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.orgCommand 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
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 MyTrigger1$ sfdx force:apex:trigger:create -n MyTrigger -s Account -e 'before insert,after insert'1$ sfdx force:apex:trigger:create -n MyTrigger -d triggersCommand 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