+ Start a Discussion
mike_merinomike_merino 

custom metadata empty in managed package

For security reasons, I am using a protected custom metadata field to hold sensitive information in a managed package.  

I built a custom metadata type called Rest_id__mdt with one custom field: Value__c.  When I click "Manage Rest Ids" I entered values for username, client_secret, etc.

Within an apex class I have this code:
------------------------------------------
      Map<String,String> ridMap = new Map<String,String>();
      for (Rest_Id__mdt r2 : [SELECT MasterLabel, QualifiedApiName, value__c FROM Rest_Id__mdt]) 
      {
   //      System.debug(r2.MasterLabel + ' : ' +  r2.value__c +' -- ' +  r2.QualifiedApiName );
         ridMap.put(r2.MasterLabel, r2.value__c);
      }
    String clientId = ridMap.get('Client Id');
    String clientSecret = ridMap.get('Client Secret');
    String username = ridMap.get('Username');   // target
    String password = ridMap.get('pw+token');   // target
    String endpoint1 = ridMap.get('Endpoint');
    String endpoint2 = ridMap.get('Endpoint2');
  
    system.debug('### grant access - clientid '+clientid);
    String reqbody = 'grant_type=password&client_id='+clientId+'&client_secret='+clientSecret+'&username='+username+'&password='+password;
------------------------------------------
the debug statement in the source org, shows that this code correctly assigns the String variables with custom metadata data. Here is the debug log for this, where Vantiv1 is the namespace
14:43:49:039 SOQL_EXECUTE_BEGIN [27]|Aggregations:0|SELECT MasterLabel, QualifiedApiName, Vantiv1__Value__c FROM Rest_Id__mdt
14:43:49:041 SOQL_EXECUTE_END [27]|Rows:6

I built a managed package with this but in my installed org, in dev console I see this
14:55:53:003 SOQL_EXECUTE_BEGIN [27]|Aggregations:0|SELECT MasterLabel, QualifiedApiName, Vantiv1__Value__c FROM Rest_Id__mdt
14:55:53:005 SOQL_EXECUTE_END [27]|Rows:0

and I get the error:
System.NullPointerException: Argument 1 cannot be null
(
Vantiv1)
',}

so this works in source org but when I installed it in another dev org, I get this error and I THINK it is related to the custom metadata type.  Any thoughts?
 
Best Answer chosen by mike_merino
mike_merinomike_merino
Salesforce support helped me with this;
=====================
"You cannot add custom settings data in managed package. You have to write post install script to add data after package is installed.

You can add data for custom metadata but you have to explicitly add custom metadata type records in the package. Just adding custom metadata type itself will not add data.

And that's also one of the main differences in custom metadata and custom settings. You can package custom metadata records but not custom settings' records."

===================
so I went back to using the custom metadata type and in the managed package manually added my 6 records (e.g. client_id, username) and now life is again lovely.  Happy winter's day!