How to populate a custom setting within a Salesforce package

How to populate a custom setting within a Salesforce package?

By default, Salesforce packages and change sets can only contain configuration, not data.

In this post, we explore how you can work around this.

Empty packages

There are various good reasons that Salesforce allows you to package configuration but not data.

For example, imagine your package contained duplicate information or added data into your customer's environment they did not want? Your package could then potentially contain a fictional account called 'Acme', which could end up with very confused salespeople trying to contact a Contact at this 'new' customer.

However, there may be a compelling reason to have your package install with some information pre-filled and in that instance, you need to find a way to add this data to the customer's ORG.

When would I need to do this?

  • For example, if you wanted to show an example record in your new app. 
  • Or, if your app required some settings to be completed before it worked then you may need to pass data across to it.

Enter the Post Install Class

Edit Package screen in Salesforce

Luckily, there is a tried and tested way of achieving this in Salesforce. 

During the packaging process, a package creator can specify a Post Install Apex class that will run whenever the package is installed. You create the Apex Class in the normal way, and that on the package edit screen specify this Apex Class as being the one that runs.

Sample Post Install Script

I am currently using a Post Install script to populate some custom settings after installation (to save the customer from having to do anything manually) but there are lots of other uses too. Try it out on a simple package using the guidance from the Salesforce Developers site.

Icon from Iconfinder, artist Nick Frost