post install script

How Does a Post Install Script Work? - Salesforce Developer Guide

Post-Install Script is the same as other apex classes. And the apex class which is executed when a managed package is installed or upgraded. This class executes the InstallHandler interface. 

global interface InstallHandler {
  void onInstall(InstallContext context)
}

This interface contains a single method named onInstall which determines what is to be performed on installation. And the onInstall method takes a context object as an argument, which gives you these informations:

  • Organization Id of the org in which the package installation occurs.
  • User Id of the user who started the installation.
  • Version Number
  •  Installation is an update or not.
  •  Installation is pushed or not.
global interface InstallContext { 
  ID organizationId();
  ID installerId();
  Boolean isUpgrade();
  Boolean isPush(); 
  Version previousVersion();
}

dont miss out iconDon't forget to check out: DML Statements vs Database Methods in Salesforce

System.version: The method of this class is used to fetch the version of a managed package and comparison version of the package. Let’s discuss the package version, It is a no. that recognizes the set of segments/components transferred in a managed package.

Format of the version number: majorNumber.minorNumber.patchNumber Example: (1.2.0). When non-patch releases major and minor numbers will increase to a selected value. And patch number of zero is used to increase the major and minor numbers.

Let’s compare the recent version with the specified version. This will return the following information:

  • Zero, If both versions are equivalent.
  • Returns greater than zero, if the specified version is smaller than the current package version.
  • It returns less than zero if the specified version is greater than the current package version.

If you want to compare the two-section version with the three-section version then patch number is ignored and this comparison only depends on the major and minor numbers.

Example of Post Install Script:

Let’s take an example of Post Install Script for a better understanding of this.

global class PostInstallScriptExample implements Messaging.inboundEmailHandler{
    global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, 
                                                           Messaging.InboundEnvelope envelope ) 
    {
        Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
        List<Contact> conList  =  new List <contact>();
        List<Lead> leadList = new List <lead>();
        String mySubject = email.subject.toLowerCase();
        String s = 'PostInstallScriptExample';
        Boolean bool;
        bool = mySubject.contains(s);
        if(bool == true){
            try{
                for(Contact con : [select Id, Name, Email, HasOptedOutOfEmail
                                   From Contact
                                   where Email = :envelope.fromAddress
                                   AND hasOptedOutOfEmail = false
                                   LIMIT 100]) {
                                       con.hasOptedOutOfEmail = true;
                                       conList.add(con);
                                   }
                update conList;
            }
            catch (System.QueryException e) {
                System.debug('Contact Query Issue: ' + e);
            } 
            try {           
                for (Lead leads : [select Id, Name, Email, HasOptedOutOfEmail from lead
                               where Email =: envelope.fromAddress and isConverted = false and hasOptedOutOfEmail = false
                               limit 100]) {   
                                   leads.hasOptedOutOfEmail = true;
                                   leadList.add(leads);
                                   System.debug('Lead Records: ' + leads);   
                               } 
                update leadList;
            }
            
            catch (System.QueryException e) {
                System.debug('Lead Query Issue: ' + e);
            }   
            
            System.debug('Found the postInstallScriptExample word in the subject line.');
        } 
        else {
            System.debug('No postInstallScriptExample word found in the subject line.' );
        }
        // Return True and exit.
        result.success = true;
        return result;
    }
}

dont miss out iconCheck out another amazing blog by Shweta here: System Mode and User Mode in Salesforce

Let’s write a test class for the above PostInstallScripteExample class:

@isTest
private class PostInstallScripteExampleTest{
    static testMethod void test1() {
        // Create a new email and envelope object.
        Messaging.InboundEmail email = new Messaging.InboundEmail() ;
        Messaging.InboundEnvelope env = new Messaging.InboundEnvelope();
        Lead l = new lead(firstName='John', 
                          lastName='Smith',
                          Company='Salesforce', 
                          Email='[email protected]', 
                          HasOptedOutOfEmail=false);
        insert l;
        Contact c = new Contact(firstName='john', 
                                lastName='smith', 
                                Email='[email protected]', 
                                HasOptedOutOfEmail=false);
        insert c;
        email.subject = 'test Post Install Script';
        env.fromAddress = '[email protected]';
        PostInstallScripteExample Obj = new PostInstallScripteExample();
        Obj.handleInboundEmail(email, env );
    }
}

Reference: salesforce.stackexchange

Popular Salesforce Blogs