This is, in some sense, a followup to this earlier question:
What is a protected custom setting record?
I got rid of the Apex class that held my config data. Instead, I created a Protected Custom Setting with the data.
This is the API name of the Custom Setting:
PrivateIncInfo__SameDayConfigurationModel__c
And this is the API name of the field:
PrivateIncInfo__xApiKey__c
So I assumed I could replace the old Apex class with the new Custom Setting:
HttpRequest request = new HttpRequest();
request.setBody(payLoad);
request.setEndPoint(endpoint);
request.setMethod('POST');
request.setHeader('Accept', 'application/json');
request.setHeader('Content-Type', 'application/json');
request.setHeader('x-api-key', PrivateIncInfo__SameDayConfigurationModel__c.PrivateIncInfo__xApiKey__c);
But when I try to add this code to my Salesforce managed package, I get this error:
Failures - 1 PrivcoImportController: Method does not exist or incorrect signature: void setHeader(String, Schema.SObjectField) from the type System.HttpRequest
I defined PrivateIncInfo__xApiKey__c as a text box, and so I assume the value is a string. But apparently I'm not getting a string value when I call it? How do I fix this?
[ [ UPDATE ] ]
I'm sorry I was unclear. I have now tried calling the data like this.
I established that as a Protected Custom Setting, using List format. I'm not sure if that is correct.
This is part of a Managed Package.
I simply need to store an API key that works to access a 3rd party API.
I tried:
PrivateIncInfo__SameDayConfigurationModel__c pcm;
try {
List<PrivateIncInfo__SameDayConfigurationModel__c> lpcm = PrivateIncInfo__SameDayConfigurationModel__c.getall().values();
pcm = lpcm[0];
request.setBody(payLoad);
request.setEndPoint(endpoint);
request.setMethod('POST');
request.setHeader('Accept', 'application/json');
request.setHeader('Content-Type', 'application/json');
request.setHeader('x-api-key', pcm.PrivateIncInfo__xApiKey__c);
request.setHeader('Authorization', ps.APIAuthorizationKey__c);
But I got an Exception. I was told the list was empty.
I also tried:
pcm = PrivateIncInfo__SameDayConfigurationModel__c.getInstance();
But when I do this, then pcm.PrivateIncInfo__xApiKey__c is null.
So what is the right answer?
This also comes says "Second argument can not be null."
request.setHeader('Content-Type', 'application/json');
request.setHeader('x-api-key', PrivateIncInfo__SameDayConfigurationModel__c.getOrgDefaults().PrivateIncInfo__xApiKey__c);