Newer Version Available

This content describes an older version of this product. View Latest

Example of a Post Install Script

The following sample post install script performs these actions on package install/upgrade.
  • 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”.
1public class PostInstallClass implements InstallHandler {
2  global void onInstall(InstallContext context) {
3    if(context.previousVersion() == null) {
4      Account a = new Account(name='Newco');
5      insert(a);
6      
7      Survey__c obj = new Survey__c(name='Client Satisfaction Survey');
8      insert obj;
9               
10      User u = [Select Id, Email from User where Id =:context.installerID()];   
11      String toAddress= u.Email;
12      String[] toAddresses = new String[]{toAddress};
13      Messaging.SingleEmailMessage mail = 
14        new Messaging.SingleEmailMessage();
15      mail.setToAddresses(toAddresses);
16      mail.setReplyTo('support@package.dev');
17      mail.setSenderDisplayName('My Package Support');
18      mail.setSubject('Package install successful');
19      mail.setPlainTextBody('Thanks for installing the package.');
20      Messaging.sendEmail(new Messaging.Email[] { mail });
21      }
22    else
23      if(context.previousVersion().compareTo(new Version(1,0)) == 0) {
24      Survey__c obj = new Survey__c(name='Upgrading from Version 1.0');
25      insert(obj);
26      }
27    if(context.isUpgrade()) {
28      Survey__c obj = new Survey__c(name='Sample Survey during Upgrade');
29      insert obj;
30      }
31    if(context.isPush()) {
32      Survey__c obj = new Survey__c(name='Sample Survey during Push');
33      insert obj;
34      }
35    }
36  }
You can test a post install script using the new testInstall method of the Test class. This method takes the following arguments.
  • 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.
This sample shows how to test a post install script implemented in the PostInstallClass Apex class.
1@isTest
2static void testInstallScript() {
3  PostInstallClass postinstall = new PostInstallClass();
4    Test.testInstall(postinstall, null);
5    Test.testInstall(postinstall, new Version(1,0), true);
6    List<Account> a = [Select id, name from Account where name ='Newco'];
7    System.assertEquals(1, a.size(), 'Account not found');
8  }