How to write a post-install script for the salesforce-managed package?

How to write a post-install script for the salesforce-managed package?

Post Install Script in Salesforce


4 min read

In this article, we will learn about what is a post-install script in Salesforce and how to write a post-install script and add it to the Salesforce managed package.

What is a post-install script?

A post-install script is an Apex Controller which is executed after the package is installed into the Salesforce org of the user.

Suppose you as a Salesforce developer have a requirement to create some sample data in an org or schedule an apex class to run on specific time intervals or you want to notify some external system after your package is installed by the end-user then this can be achieved with the help of a post-install script.

In order to achieve this we have to implement the InstallHandler interface. The InstallHandler interface has a single method called onInstall, which specifies the actions to be performed on install/upgrade.

Example Scenario

For writing a post-install script let's consider a scenario where you as a Salesforce developer want to create a custom settings record once the user installs the package.

As you can see in the image below we have a list custom setting named State with a namespace prefix Test20052023. It also has 2 fields named State and abbreviation.

We will use this custom setting to store the state name along with the abbreviation.

Example of Post-Install Script

Create an apex class named CustomSettingInstaller in your org and paste the following code.

global class CustomSettingInstaller implements InstallHandler{
    global void onInstall(InstallContext context){
//Creating a record for custom setting named State        
List<Test20052023__State__c> csettings = new List<Test20052023__State__c>();
        Test20052023__State__c csetting = new Test20052023__State__c();
        csetting.Test20052023__State__c = 'Madhya Pradesh';
        csetting.Test20052023__Abbreviation__c = 'MP';
        csetting.Name = 'ABC';
        insert csettings;    

In order to create a managed package and submit it to AppExchange for security review our apex class should also have a test class and the code coverage should be more than 75%. So create an apex class named CustomSettingInstallerTest and paste the following code.

public class CustomSettingInstallerTest {
    static void testOnInstall(){
        CustomSettingInstaller customSettingInstaller = new CustomSettingInstaller();
        Test20052023__State__c csetting = [Select id,Name from Test20052023__State__c where name='ABC' limit 1];
        System.assertEquals('ABC', csetting.Name);

After creating both the apex classes click on the run test button and verify the code coverage as highlighted in the below image.

How to add a post-install script to managed package?

In order to add a post-install script to managed package follow the below steps.

  1. Search for the package manager from setup and click on edit for the package to which you want to add the post-install script.

  2. After you click on the edit button a page will open from where you can select the post-install script.

  3. Click on the save button.

  4. Now you can upload the package by going to that specific package from the package manager.

Verify Results

In order to verify results you have to install the package in another org. Once you have installed the package the package name will be visible in the installed packages list as highlighted in the image below.

Now search for custom settings from setup and you will see the State custom setting with namespace Test20052023 as highlighted in the image below.

Now click on the manage button as highlighted in the above image. You will see a custom-setting record named ABC which is created with the help of a post-install script.

Now click on the name of the custom setting record and you will see all the fields being populated.

Also, check out the below video on What is Post Install Script in Salesforce and how to write it.


In short, a post-install script is an apex class that is used to perform tasks like creating data, custom setting records or scheduling an apex class, etc.,

We have to implement the onInstall method of the InstallHandler interface.

You can learn more about InstallHandler from here.

I hope you find this article helpful.

Please don't forget to like, comment and share and you can also check out my other articles. Also, don't forget to subscribe to my newsletter so you can be notified about new articles.

I am looking to get into technical writing so if you want me to write an article then please connect with me on my Twitter handle here.

Let me also know which topic I should write about next.

Did you find this article valuable?

Support shubham lashkan by becoming a sponsor. Any amount is appreciated!