---
schema_version: "1"
id: c0dc0d3a5541ec20edf0b7ddccab277f38a1bd5944db8b7e9dc9acb24f1369f8
title: Example of a Post Install Script
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform-pkg-exchange/managed_packaging/guides/first-generation/apex-post-install-script-create.md
canonical_url: https://developer.salesforce.com/docs/platform/pkg2-dev/guide/apex-post-install-script-create.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: fbe30843701c1df2b3db0ae72a9d9611f28d146ba984638be1f60d8d6b862d83
products:
  - Second-Generation Managed Packaging Developer Guide
  - merged-docs
---

# 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”.

```apex
public class PostInstallClass implements InstallHandler {
  global void onInstall(InstallContext context) {
    if(context.previousVersion() == null) {
      Account a = new Account(name='Newco');
      insert(a);

      Survey__c obj = new Survey__c(name='Client Satisfaction Survey');
      insert obj;

      User u = [Select Id, Email from User where Id =:context.installerID()];   
      String toAddress= u.Email;
      String[] toAddresses = new String[]{toAddress};
      Messaging.SingleEmailMessage mail =
        new Messaging.SingleEmailMessage();
      mail.setToAddresses(toAddresses);
      mail.setReplyTo('support@package.dev');
      mail.setSenderDisplayName('My Package Support');
      mail.setSubject('Package install successful');
      mail.setPlainTextBody('Thanks for installing the package.');
      Messaging.sendEmail(new Messaging.Email[] { mail });
      }
    else
      if(context.previousVersion().compareTo(new Version(1,0)) == 0) {
      Survey__c obj = new Survey__c(name='Upgrading from Version 1.0');
      insert(obj);
      }
    if(context.isUpgrade()) {
      Survey__c obj = new Survey__c(name='Sample Survey during Upgrade');
      insert obj;
      }
    if(context.isPush()) {
      Survey__c obj = new Survey__c(name='Sample Survey during Push');
      insert obj;
      }
    }
  }

```

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.

```apex
@isTest
static void testInstallScript() {
  PostInstallClass postinstall = new PostInstallClass();
    Test.testInstall(postinstall, null);
    Test.testInstall(postinstall, new Version(1,0), true);
    List<Account> a = [Select id, name from Account where name ='Newco'];
    System.assertEquals(1, a.size(), 'Account not found');
  }

```
