You are here:
Define Apex Classes
Salesforce stores Apex classes as metadata.
Required Editions
| Available in: Salesforce Classic (not available in all orgs) |
| Available in: Performance, Unlimited, Developer, Enterprise, and Database.com Editions |
| User Permissions Needed | |
|---|---|
| To define, edit, delete, set security, and set version settings for Apex classes: | Author Apex |
| To run Apex tests: | View Setup and Configuration |
You can add, edit, or delete Apex using the Salesforce user interface only in a Developer
Edition organization, a Salesforce Enterprise Edition trial organization, or sandbox
organization. In a Salesforce production organization, you can change Apex only by using the
Metadata API deploy call, the Salesforce Extensions for
Visual Studio Code, or the Ant Migration Tool. The Salesforce Extensions for Visual Studio Code
and Ant Migration Tool are free resources provided by Salesforce to support its users and
partners, but are not considered part of our Services for purposes of the Salesforce Main
Services Agreement.
- From Setup, enter Apex Classes in the Quick Find box, then select Apex Classes.
- Click New.
-
Click Version Settings to specify the version of Apex and the API
used with this class.
If your organization has installed managed packages from the AppExchange, you can also specify which version of each managed package to use with this class. Use the default values for all versions. This associates the class with the most recent version of Apex and the API, as well as each managed package. You can specify an older version of a managed package if you want to access components or functionality that differs from the most recent package version. You can specify an older version of Apex and the API to maintain specific behavior.
-
In the class editor, enter the Apex code for the class. A single class can be up to 1
million characters in length, not including comments, test methods, or classes defined using
@isTest. - Click Save to save your changes and return to the class detail screen, or click Quick Save to save your changes and continue editing your class. Your Apex class must compile correctly before you can save your class.
Once saved, classes can be invoked through class methods or variables by other Apex code, such as a trigger.
isValid flag that is set to true as long as dependent metadata has not changed since the class was last compiled.
If any changes are made to object names or fields that are used in the class, including
superficial changes such as edits to an object or field description, or if changes are made to a
class that calls this class, the isValid flag is set to
false. When a trigger or Web service call invokes the
class, the code is recompiled and the user is notified if there are any errors. If there are no
errors, the isValid flag is reset to true. 
