Run Apex on Package Install/Upgrade
App developers can specify an Apex script to run automatically
after a subscriber installs or upgrades a managed package. This script makes it possible to customize the package
install or upgrade, 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. For simplicity, you can only specify one post install script. It must be an
Apex class that is a member of the package.
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 can only see this user at runtime, not while running tests.
If the script fails, the install/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/upgrade details are unavailable.
The post install script has the following additional properties.
- 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 or inherit sharing keyword. These keywords can prevent the package from successfully installing. To learn more, see the Apex Developer Guide.