Newer Version Available
Create an Apex Class
You can create Apex classes from the CLI.
By default, the class is created in the directory from which you run the command. The
standard DX project template assumes you’ll create your Apex classes in the
<package directory>/force-app/main/default/classes
directory. To create classes in this directory, change to it before running the command.
1sfdx force:apex:class:create -n myclassIf you’re in a different directory, indicate the -d parameter to specify the absolute or relative path to the directory where you want to save your Apex class files. If you don’t indicate an absolute or a relative path and the directory doesn’t exist, the CLI attempts to create it for you.
1sfdx force:apex:class:create -n myclass -d ../force-app/main/default/classesThe command generates two files:
- myclass.cls-meta.xml—metadata format
- myclass.cls—Apex source file
By default, the command creates a shell for an Apex class. However, you can select different templates depending on what you’re creating.
| Template | Description | More Information in Apex Developer Guide |
|---|---|---|
| DefaultApexClass (default) | Standard Apex class. | Classes |
| ApexException | Use Apex built-in exceptions or create custom exceptions. All exceptions have common methods. | Exception Class and Built-in Exceptions |
| ApexUnitTest | Use the @isTest annotation to define classes and methods that only contain code used for testing your application. | isTest Annotation |
| InboundEmailService | Use email services to process the contents, headers, and attachments of inbound email. | Apex Email Service |