Sub topic 12
Package Version References and Version-Aware Apex
This chapter assumes you know how to read a MAJOR.MINOR.PATCH.BUILD version number, can install a package version into an org, and can read Apex and component metadata files. It covers how a component records the package version it was saved against, and how managed package code can behave differently for older callers.
Why versions matter to callers
When you install a managed package, other code in your org can call its global Apex classes and methods. The package author will eventually ship a newer version, and that version may change what a method returns. If every caller silently got the new behavior, existing code could break after an upgrade.
Salesforce handles this with two mechanisms:
- A package version reference, stored with each component, records which version of the package the component was saved against.
- Version-aware Apex lets the package code check which version the caller is using and respond accordingly.
Package version references
A package version reference is stored in the -meta.xml file of a component, in the packageVersions element. Each entry names an installed managed package by its namespace and gives the major and minor version number that was installed when you saved the component.
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>61.0</apiVersion>
<packageVersions>
<majorNumber>1</majorNumber>
<minorNumber>0</minorNumber>
<namespace>acme</namespace>
</packageVersions>
<status>Active</status>
</ApexClass>This class was saved against version 1.0 of the package with the namespace acme. Only the major and minor numbers are recorded. Patch and build numbers are not part of the reference.
The reference has two effects:
- The class keeps seeing the behavior of version 1.0 of
acme's global API, even after the org is upgraded to a later version of the package. - The reference constrains what you can do with the package. An installed version that a component still references cannot simply be removed or downgraded below that level.
To move a component to a newer version, you update the reference deliberately, for example by changing the version in the component's settings and saving it again.
Version-aware Apex
Code inside a managed package can call System.requestVersion(). It returns a System.Version object for the package version that the calling code is using. The package author compares that value to decide which behavior to return.
global with sharing class Greeter {
global static String greet() {
if (System.requestVersion().compareTo(new Version(2, 0)) < 0) {
return 'Hello';
}
return 'Hello, welcome';
}
}A caller that references version 1.0 gets Hello. A caller that references version 2.0 or later gets Hello, welcome. The package author can therefore change behavior in version 2.0 without breaking subscribers who have not moved their code forward.
Common errors
- Expecting the behavior to change after an upgrade. Upgrading the installed package does not change the version reference in your classes. They keep the older behavior until you update the reference.
- Calling
requestVersion()outside a package. The method is useful only in code that ships in a managed package. In unpackaged code, there is no separate package version to report. - Comparing against the wrong number.
new Version(2, 0)means major 2, minor 0. A caller on 1.9 is below it, and a caller on 2.0 is not. - Ignoring the reference when removing versions. If components still reference an older version, plan the update of those components before you rely on a newer version.
Exercise
Exercise
A subscriber's Apex class has a packageVersions entry for namespace acme with majorNumber 1 and minorNumber 0. The org is then upgraded to acme 2.0, which contains the Greeter class shown above. What does Greeter.greet() return when the subscriber's class calls it, and what must the subscriber do to get the newer result?
Show answer
It returns "Hello", because System.requestVersion() reports 1.0 for that caller, which is below 2.0. To get "Hello, welcome", the subscriber must update the class's package version reference to 2.0 or later and save it.