Newer Version Available
InstallHandler Interface
Namespace
Usage
App developers can implement this interface to specify Apex code that runs automatically after a subscriber installs or upgrades a managed package. The package install or upgrade can be customized based on details of the subscriber’s organization. For instance, you can use the script to populate custom settings, create sample data, send an email to the installer, notify an external system, or kick off a batch operation to populate a new field across a large set of data.
The post install script is invoked after tests have been run, and is subject to default governor limits. It runs as a special system user that represents your package, so all operations performed by the script appear to be done by your package. You can access this user by using UserInfo. You only see this user at runtime, not while running tests.
If the script fails, the install or upgrade is aborted. Any errors in the script are emailed to the user specified in the Notify on Apex Error field of the package. If no user is specified, the install or upgrade details are unavailable.
- It can initiate batch, scheduled, and future jobs.
- It can’t access Session IDs.
- It can only perform callouts using an async operation. The callout occurs after the script is run and the install is complete and committed.
- It can’t call another Apex class in the package if that Apex class uses the with sharing keyword. This keyword can prevent the package from successfully installing. See the Apex Developer Guide to learn more.
- 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). The version is always a three-part number, such as 1.2.0.
- Whether the installation is an upgrade.
- Whether the installation is a push.
InstallHandler Example Implementation
-
If the previous version is null, that is, the package is being installed for the first time, the script:
- Creates a new Account called Newco and verifies that it was created.
- Creates a new instance of the custom object Survey, called Client Satisfaction Survey.
- Sends an email message to the subscriber confirming installation of the package.
- If the previous version is 1.0, the script creates a new instance of Survey called ”Upgrading from Version 1.0”.
- If the package is an upgrade, the script creates a new instance of Survey called ”Sample Survey during Upgrade”.
- If the upgrade is being pushed, the script creates a new instance of Survey called ”Sample Survey during Push”.
- A class that implements the InstallHandler interface.
- A Version object that specifies the version number of the existing package.
- An optional Boolean value that is true if the installation is a push. The default is false.