You need to sign in to do that
Don't have an account?

Can't access named credential from Apex code within a package in subscriber org
I have a package that contains apex code using a named credential to access an external REST service. The code works fine in the dev org, but once packaged and deployed to a test (subscriber) org I'm getting a System.CalloutException with the following message:
The named credential is not included in the package as the endpoint configuration needs to be different for each org, however I've created the named credential in the test (subscriber) org, exactly as I have it in the dev org. The documentation on the use of named credentials in Apex code within packages suggests that this should work fine:
<excerpt>
Ref: http://www.salesforce.com/us/developer/docs/packagingGuide/Content/packaging_component_behavior.htm
My Apex code:
// Setup the REST call
Http http = new Http();
String endpointURL = 'callout:My_Named_Credential'; // Named Credential
// Establish the request
HttpRequest reqData = new HttpRequest();
reqData.setTimeout(20000);
reqData.setEndpoint(endpointURL);
reqData.setMethod('POST');
// Attempt the API callout
HTTPResponse res = http.send(reqData);
Has anyone been able to successfully get this working? Are there any work-arounds I need to be aware of?
Thanks in advance,
BW.
The callout couldn't access the endpoint. You might not have the required permissions, or the named credential "My_Named_Credential" might not exist.
The named credential is not included in the package as the endpoint configuration needs to be different for each org, however I've created the named credential in the test (subscriber) org, exactly as I have it in the dev org. The documentation on the use of named credentials in Apex code within packages suggests that this should work fine:
<excerpt>
If you package Apex code that specifies a named credential as a callout endpoint, also add the named credential to that package or otherwise make sure that the subscriber organization has a valid named credential with the same name.
</excerpt>Ref: http://www.salesforce.com/us/developer/docs/packagingGuide/Content/packaging_component_behavior.htm
My Apex code:
// Setup the REST call
Http http = new Http();
String endpointURL = 'callout:My_Named_Credential'; // Named Credential
// Establish the request
HttpRequest reqData = new HttpRequest();
reqData.setTimeout(20000);
reqData.setEndpoint(endpointURL);
reqData.setMethod('POST');
// Attempt the API callout
HTTPResponse res = http.send(reqData);
Has anyone been able to successfully get this working? Are there any work-arounds I need to be aware of?
Thanks in advance,
BW.
Try to prefix the name of your named credential with ".__" as shown in the sample below.
I found this answer in the Salesforce Packaging Guide (https://help.salesforce.com/help/pdfs/en/salesforce_packaging_guide.pdf)
Cheers,
Eric
All Answers
Try to prefix the name of your named credential with ".__" as shown in the sample below.
I found this answer in the Salesforce Packaging Guide (https://help.salesforce.com/help/pdfs/en/salesforce_packaging_guide.pdf)
Cheers,
Eric
This is the same advice I got from Salesforce support. One catch is that using the local namespace with the '.__' breaks the code in the developer org as there is no way to create a Named Credential outside the package namespace. So the only solution I could determine to resolve the issue in both dev org and subscriber org is to check the org Id to see if we were running in the dev org and remove the name space if we are, as below:
Thanks again,
BW.
You can simply use the following code: And here is the code of the Utility Class: I hope this helps. And thanks a lot Eric for your answer it helped me a lot.
Regards,
Jheel