You are here:
Manage Apex Classes
An Apex class is a template or blueprint from which Apex objects are created. Classes consist of other classes, user-defined methods, variables, exception types, and static initialization code.
Required Editions
| Available in: Performance, Unlimited, Developer, and Enterprise 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 |
Once successfully saved, class methods or variables can be invoked
by other Apex code, or through the SOAP API (or AJAX Toolkit) for methods that have
been designated with the webService
keyword.
The Apex Classes page enables you to create and manage Apex classes. To access the Apex Classes page, from Setup, enter Apex Classes in the Quick Find box, then select Apex Classes. For additional development functionality, use the Developer Console.
To create an Apex class, from the Apex Classes page, click New and write your Apex code in the editor.
While developers can write class methods according to the syntax outlined in the Apex Code Developer's Guide, classes can also be automatically generated by consuming a WSDL document that is stored on a local hard drive or network. Creating a class by consuming a WSDL document allows developers to make callouts to the external Web service in their Apex. From the Apex Classes page, click Generate From WSDL to generate an Apex class from a WSDL document.
Dynamic Apex Classes are dynamically generated to provide greater access into existing platform features. For example, if you register an API specification with External Services, all of the dynamically generated classes are listed under the Dynamic Apex Classes heading on the Apex Classes page. For the original source, click Open. Dynamic Apex Classes don't contribute toward the “Maximum amount of code used by all Apex code in an org” limit and are exempt from code coverage requirements.
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.
Once you have created an Apex class, you can perform various actions.
- Click Edit next to the class name to modify its contents in a simple editor.
- Click Del next to the class name to delete the class from your
organization.
Note- You cannot delete a class that is specified as a controller for a Visualforce page or component.
- A
icon indicates that an Apex class was released in a managed package. Apex
classes in packages have special considerations. For more information, see the Use Managed Packages to Develop Your AppExchange
Solution. - A
icon indicates that an Apex class is in an installed managed package. You
cannot edit or delete a class in a managed package. - A
icon indicates that an Apex class in a previously released managed package
will be deleted on the next package upload. You can choose to undelete the Apex
class through the package detail page.
- If an Apex class has any methods defined as a
webService, you can click WSDL next to the class name to generate a WSDL document from the class contents. This document contains all of the information necessary for a client to consume Apex Web service methods. All class methods with thewebServicekeyword are included in the resulting WSDL document. - Click Security next to the class name to select the profiles that
are allowed to execute methods in the class from top-level entry points, such as Web service
methods. For classes that are installed in your organization as part of a managed package,
this link only displays for those defined as
global. - Click Estimate your organization's code coverage to find out how much of the Apex code in your organization is currently covered by unit tests. This percentage is based on the latest results of tests that you’ve already executed. If you have no test results, code coverage will be 0%.
- If you have unit tests in at least one Apex class, click Run All Tests to run all the unit tests in your organization.
- Click Compile all classes to compile all the Apex classes in your organization. If you have classes that are installed from a managed package and that have test methods or are test classes, you must compile these classes first before you can view them and run their test methods from the Apex Test Execution page. Managed package classes can be compiled only through the Compile all classes link because they cannot be saved. Otherwise, saving Apex classes that aren't from a managed package causes them to be recompiled. This link compiles all the Apex classes in your organization, whether or not they are from a managed package.
- Click Compile only invalid classes to recompile only invalid Apex classes in your organization. Salesforce automatically identifies only classes that require recompilation, which reduces compilation overhead and minimizes potential runtime errors. Routinely perform this targeted recompilation to prevent runtime delays during peak traffic. If the compilation is successful, you see a confirmation message. If the compilation is unsuccessful, you see a list of problems and warnings for each affected class.
