Newer Version Available

This content describes an older version of this product. View Latest

ApexClass

Represents an Apex class. 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.

For more information, see the Lightning Platform Apex Code Developer's Guide. This type extends the MetadataWithContent metadata type and inherits its content and fullName fields.

By default, you can’t deploy updates to an Apex class if there are one or more active jobs for that class. To deploy updates in this case, do one of the following.

  • Cancel Apex jobs before deploying changes to Apex code. Reschedule the jobs after the deployment.
  • Enable deployments with Apex jobs in the Salesforce user interface in the Deployment Settings page.

Note

Supported Calls

All Metadata API calls except CRUD-Based Calls, which prevents deployment outside of proper deployment lifecycle and test-execution constraints.

Declarative Metadata File Suffix and Directory Location

The file suffix is .cls for the class file. The accompanying metadata file is named ClassName.cls-meta.xml.

Apex classes are stored in the classes folder in the corresponding package directory.

Version

Apex classes are available in API version 10.0 and later.

Fields

This metadata type contains the following fields:

Field Name Field Type Description
apiVersion double

The API version for this class. Every class has an API version specified at creation.

content base64 The Apex class definition. Base 64-encoded binary data. Before making an API call, client applications must encode the binary attachment data as base64. Upon receiving a response, client applications must decode the base64 data to binary. This conversion is handled for you by a SOAP client. This field is inherited from the MetadataWithContent component.
fullName string The Apex class name. The name can only contain characters, letters, and the underscore (_) character, must start with a letter, and can’t end with an underscore or contain two consecutive underscore characters. This field is inherited from the Metadata component.
packageVersions PackageVersion[] The list of installed managed package versions that are referenced by this Apex class.

For more information about managed packages, see Second-Generation Managed Packages in the Salesforce DX Developer Guide. This field is available in API version 16.0 and later.

status ApexCodeUnitStatus (enumeration of type string)

The status of the Apex class. The following string values are valid:

  • Active - The class is active.
  • Deleted - The class is marked for deletion. This value is useful for managed packages, because it allows a class to be deleted when a managed package is updated.

ApexCodeUnitStatus includes an Inactive option, but it’s only supported for ApexTrigger; it isn’t supported for ApexClass.

PackageVersion

PackageVersion identifies a version of a managed package. A package version is a number that identifies the set of components included in a package. The version number has the format majorNumber.minorNumber.patchNumber (for example, 2.1.3). The major and minor numbers increase to a chosen value during every major release. The patchNumber is generated and updated only for a patch release. It’s available in API version 16.0 and later.

Field Name Field Type Description
namespace string Required. In a packaging context, a namespace prefix is a one to 15-character alphanumeric identifier that distinguishes your package and its contents from packages of other developers on AppExchange. Namespace prefixes are case-insensitive. For example, ABC and abc aren’t recognized as unique. Your namespace prefix must be globally unique across all Salesforce orgs.

Salesforce automatically prepends your namespace prefix, followed by two underscores (“__”), to all unique component names in your Salesforce organization. A unique package component is one that requires a name that no other component has within Salesforce, such as custom objects, custom fields, custom links, s-controls, and validation rules. For more information about namespaces, see Create and Register Your Namespace in the Second-Generation Managed Packaging Developer Guide.

majorNumber int Required. The major number of the package version. A package version number has a majorNumber.minorNumber format.
minorNumber int Required. The minor number of the package version. A package version number has a majorNumber.minorNumber format.

Declarative Metadata Sample Definition

The following sample creates the MyhelloWorld.cls class, and the corresponding MyHelloWorld.cls-meta.xml metadata file.

MyHelloWorld.cls file:

1public class MyHelloWorld {
2// This method updates the Hello field on a list
3// of accounts.
4public static void addHelloWorld(Account[] accs){
5	for (Account a:accs){
6		if (a.Hello__c != 'World')
7		a.Hello__c = 'World';
8		}
9	}
10}

MyHelloWorld.cls-meta.xml:

1<?xml version="1.0" encoding="UTF-8"?>
2<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3    <apiVersion>64.0</apiVersion>
4</ApexClass>

Wildcard Support in the Manifest File

This metadata type supports the wildcard character * (asterisk) in the package.xml manifest file. For information about using the manifest file, see Deploying and Retrieving Metadata with the Zip File.