Newer Version Available

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

Set Package Versions for Apex Classes and Triggers

As a managed package subscriber, you can specify which package version that your managed Apex classes and triggers use. Set the package version in Setup, through metadata deployments, or with API requests.

In Summer ’25 and later, package subscribers can use version settings to specify the version of a migrated second-generation managed package (2GP) that an Apex class or trigger depends on. This functionality is already available to first-generation managed packages (1GP), but isn’t yet supported in 2GP packages that weren’t converted from a 1GP package. See Apex Version Settings in Migrated Second-Generation Managed Packages (2GP).

Note

A package version is a number that identifies the set of components uploaded 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.

If you install a new package version, only one instance of each component exists in the package, but the components can emulate older versions. Say that you use a managed package that contains an Apex class. If the publisher decides to deprecate a method in the Apex class and release a new package version, you still see only one instance of the Apex class after installing the new version. However, this Apex class can still emulate the previous version for any code that references the deprecated method in the older version.

By default, an Apex class or trigger that references a managed package is associated with the package version installed when that class or trigger was last saved or deployed. For example, say that you install version 1.0 of Package A, and then create and deploy an Apex class that references that managed package. If you upgrade to version 2.0 of Package A but don’t redeploy the class, then the class remains associated with version 1.0. However, if you upgrade Package A to version 2.0 and then redeploy the class, then the class is now associated with version 2.0.

You can override the default package version settings for an Apex class or trigger. When set to a specific package version, the class or trigger views the package’s global Apex as if that version was installed. Explicitly setting a package version is useful if your class or trigger relies on an older shape of a packaged component. For an example where overriding the default package version is vital to maintain backwards compatibility, see Safely Upgrade Packages from Developer and Subscriber Perspectives.

Set a Package Version in Setup

Associate an Apex class or trigger with a specific package version in Setup.
To configure the package version settings for an Apex class or trigger:
  1. From Setup, enter Apex Classes or Apex Triggers in the Quick Find box, and then select Apex Classes or Apex Triggers.
  2. From the list, click Edit for the Apex class or trigger that you want to configure.
  3. Click the Version Settings tab.
  4. From the Version dropdown for the managed package, select the desired version referenced by the class or trigger.
    The class or trigger continues to use this version even if your install later versions of the managed package, unless you manually update the version setting.
  5. Click Save.

If the package is referenced in the class or trigger, you can’t remove a class or trigger’s version setting for a managed package. To find where the class or trigger references a managed package, on the class or trigger’s Detail page, click Show Dependencies.

Note

Set a Package Version in Metadata API

Associate an Apex class or trigger with a specific package version by using Metadata API.

With the PackageVersion field, you specify a managed package version in an Apex class or trigger’s metadata.

  1. Identify the managed package’s reference ID. This ID is either the subscriber package ID for migrated 2GP managed packages or the package namespace for 1GP managed packages.
    1. From Setup, in the Quick Find box, enter Installed Packages, and select Installed Packages.
    2. Locate the installed package that you want to reference and click the Package Name.
    3. On the Installed Package Detail page, locate the Version Setting field. The field identifies the type of reference ID.
  2. In the Apex class or trigger’s metadata file, add a <packageVersions> element. If your Apex class or trigger references multiple managed packages, include a separate <packageVersions> element for each package.
  3. Within the <packageVersions> element, add the required fields and the desired values.
    • For migrated 2GP managed packages, add the packageId field.
    • For 1GP managed packages, use the namespace field instead.

Example

Here’s an example declarative metadata definition of an Apex class that references a migrated 2GP managed package.

1<?xml version="1.0" encoding="UTF-8"?>
2<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3    <apiVersion>66.0</apiVersion>
4    <packageVersions>
5        <majorNumber>3</majorNumber>
6        <minorNumber>0</minorNumber>
7        <packageId>033xx0000000001</packageId>
8    </packageVersions>
9    <status>Active</status>
10</ApexClass>

Here’s an example declarative metadata definition of an Apex class that references a 1GP managed package.

1<?xml version="1.0" encoding="UTF-8"?>
2<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
3    <apiVersion>66.0</apiVersion>
4    <packageVersions>
5        <majorNumber>3</majorNumber>
6        <minorNumber>0</minorNumber>
7        <namespace>pkg1</namespace>
8    </packageVersions>
9    <status>Active</status>
10</ApexClass>

Metadata Usage Note for Migrated 2GP Packages

If you set a package version for an Apex class or trigger that references a 1GP managed package before Summer ’25, your existing version settings remain valid when the developer migrates the package to 2GP.

If you retrieve metadata using Salesforce API version 61.0 and earlier, <namespace> is still used in the <packageVersions> section of the meta.xml file to identify the referenced migrated 2GP package. If you retrieve metadata using API version 62.0 and later, <packageId> is used in the <packageVersions> section instead.

An error can occur if you deploy metadata using <packageId> to an org that still has the 1GP or 2GP pre-migrated version of the package installed. To resolve this issue, either upgrade the target org to the migrated package, or edit the <packageVersions> section to use <namespace> instead.

Note

Set a Package Version in API Requests

Specify a managed package version by using the package version header in your API request.
  1. Identify the managed package’s reference ID. This ID is either the subscriber package ID for migrated 2GP managed packages or the package namespace for 1GP managed packages.
    1. From Setup, in the Quick Find box, enter Installed Packages, and select Installed Packages.
    2. Locate the installed package that you want to reference and click the Package Name.
    3. On the Installed Package Detail page, locate the Version Setting field. The field identifies the type of reference ID.
    If the Version Setting field is set to packageID, then the value of the Package field is the package’s reference ID. If the Version Setting field is set to namespace, then the value of the Namespace field is the package’s reference ID.
  2. Set the package version header according to the managed package’s reference ID and the desired package version.
    • For REST API calls, use the x-sfdc-packageversion-[packageId/namespace] header. See Package Version Header in the REST API Developer Guide.
    • For SOAP API calls, use the PackageVersionHeader header. See PackageVersionHeader in the SOAP API Developer Guide.

If a package version isn’t specified in a request header, the API client uses the version of the package specified in Setup. To set this value, from Setup, enter API in the Quick Find box and select API. Then click Configure Enterprise Package Version Settings under Enterprise Package Version Settings.

Note