I am trying to Creating Custom Settings record on insert using InstallHandler.

   global class CVFC_InstallClass implements InstallHandler {

   global void onInstall(InstallContext CMLeadSetting) {
        CMLeadSetting__c cml = new CMLeadSetting__c(Name = 'CMLead Conversion');
        insert cml;


In my code, do you see anything I've missed? The custom setting is getting created but no record is being generated. I'm trying to figure out why this is happening.


Couple of items to check on this..

  1. How good is your install handler test class? Your test class should perform an insert, upgrade, push, et al. It should check for all the various conditions. Furthermore, you should have plenty of System.assert calls to verify that your custom settings and values are in fact created the way you intend them. See this for an example: https://bitbucket.org/codefriar/salesforce/src/243dbcaaa6f1057f581a68d6c7bdf0a387663307/salesforce/InstallScriptExampleTest.cls?at=master
  2. Are you preforming a push upgrade when installing the package version? If not, then your custom setting logic will not get executed.
  3. This looks like a List custom setting, correct? If not, then you'll need to include SetupOwnerId to specify whether if it's a org default hierarchy setting.
  4. You're only setting a name and no other fields on the custom setting. Are you trying to access any other fields on the custom setting record when fetching the record, and since those are not set they are null?

Agree with DancinLlama's answer, though personally though I would consider having the code reading the setting respond accordingly when no setting is present. Inserting it here (as per point 3 of the other answer) is prescribing the level the setting is inserted at. Either way, since the setting can be deleted by the admin, you need to have your code respond accordingly when the setting record does not exist. So I personally would recommend not even attempting to insert the setting record on install. Or even demand create it when you reference the setting if really need this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.