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

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**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1684640715095/a0a0b8d4-28a0-4c95-b64a-e8d0d7aba5ca.png align="center")

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.

```java
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';
        csettings.add(csetting);
        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.

```java
@isTest
public class CustomSettingInstallerTest {
    @istest
    static void testOnInstall(){
        test.startTest();
        CustomSettingInstaller customSettingInstaller = new CustomSettingInstaller();
		customsettingINstaller.onInstall(null);
        test.stopTest();
        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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1684641319559/6c20a6de-1d33-402f-94b3-4b48ccca1d17.png align="center")

### 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. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1684641601688/f5fd1d16-c667-4aff-8421-af473fa38782.png align="center")
    
    After you click on the **edit** button a page will open from where you can select the post-install script.
    
3. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1684641673419/2c44d57a-541b-465f-9a77-f0cb496bc76d.png align="center")
    
    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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1684641995565/b683e919-4dc5-447f-9239-1e28dc7815ac.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1684642110172/63900756-7a8e-470d-a483-0f8b4c665c9c.png align="center")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1684642281427/b4a9b990-4ff4-4e17-a782-a07baf24a9ad.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1684642335678/2511df00-9dcb-40b3-b17a-b3650f415b56.png align="center")

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

%[https://www.youtube.com/watch?v=6mtbBdouM1A] 

### Conclusion

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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_install_handler.htm).

I hope you find this article helpful. If this article helped you then consider buying me a coffee.

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**](https://blog.shubhamlashkan.com/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**](https://twitter.com/ShubhamLashkan).

Learning Salesforce then do checkout Salesforce Verse: [https://salesforceverse.com](https://salesforceverse.com)

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