How Does a Post Install Script Work?
global interface InstallHandler {
void onInstall(InstallContext context)
}
- The org ID of the organization in which the installation takes place.
- The user ID of the user who initiated the installation.
- The version number of the previously installed package (specified using the Version class). This is always a three-part number, such as 1.2.0.
- Whether the installation is an upgrade
- Whether the installation is a push
global interface InstallContext {
ID organizationId();
ID installerId();
Boolean isUpgrade();
Boolean isPush();
Version previousVersion();
}
Version Methods and Class
You can use the methods in the System.Version class to get the version of a managed package and to compare package versions. A package version is a number that identifies the set of components 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 non-patch release. Major and minor number increases always use a patch number of 0.
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 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’s no patch version. |
- System.requestVersion: Returns a two-part version that contains the major and minor version numbers of a package.Using this method, you can determine the version of an installed instance of your package from which the calling code is referencing your package. Based on the version that the calling code has, you can customize the behavior of your package code.
- System.runAs(System.Version): Changes the current package version to the package version specified in the argument.
When a subscriber has installed multiple versions of your package and writes code that references Apex classes or triggers in your package, they must select the version they’re referencing. You can execute different code paths in your package’s Apex code based on the version setting of the calling Apex code making the reference. You can determine the calling code’s package version setting by calling the System.requestVersion method in the package code.