Use the Version methods to get the version of a managed package of a subscriber and to compare package versions.
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.
A called component can check the version against which the caller was compiled using the System.requestVersion method and behave differently depending on the caller’s expectations. This allows you to continue to support existing behavior in classes and triggers in previous package versions while continuing to evolve the code.
The value returned by the System.requestVersion method is an instance of this class with a two-part version number containing a major and a minor number. Since the System.requestVersion method doesn’t return a patch number, the patch number in the returned Version object is null.
The System.Version class can also hold also a three-part version number that includes a patch number.
| Arguments | Description |
|---|---|
| Integer major Integer minor |
Creates a two-part package version using the specified major and minor version numbers. |
| Integer major Integer minor Integer patch |
Creates a three-part package version using the specified major, minor, and patch version numbers. |
The following are instance methods for the System.Version class.
| Method | Arguments | Return Type | Description |
|---|---|---|---|
| compareTo | System.Version version | Integer | Compares the current version with the specified version and
returns one of the following values:
If a two-part version is being compared to a three-part version, the patch number is ignored and the comparison is based only on the major and minor numbers. |
| major | Integer | Returns the major package version of the of the calling code. | |
| minor | Integer | Returns the minor package version of the calling code. | |
| patch | Integer | Returns the patch package version of the calling code or null if there is no patch version. |
if (System.requestVersion() == new Version(1,0)) { // Do something } if ((System.requestVersion().major() == 1) && (System.requestVersion().minor() > 0) && (System.requestVersion().minor() <=9)) { // Do something different for versions 1.1 to 1.9 } else if (System.requestVersion().compareTo(new Version(2,0)) >= 0) { // Do something completely different for versions 2.0 or greater }