I'm creating a managed package that includes a callout to a custom REST API endpoint. Based on this conversation thread, it appears that the "new" External Credentials are not supported for 2GP packaging: Correctly grant access to packaged Named Credentials
The existing code works well in the package org, so I'm hesitant to change the implementation. Instead, I tried to simply create the Named Credential manually in the subscriber org and grant access to it.
Here's the apex I'm using. Note there is no namespace in the Named Credential name:
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:namedCredentialNameHere');
Then I logged onto a demo/subscriber org and manually created the "namedCredentialNameHere" named credential, external credential, and permission set assignment. Then I fired the callout via anonymous apex. Got a 200 response, everything is great right?
Unfortunately, my managed package code doesn't get the same result. When the managed package apex code fires, this exception occurs:
The callout couldn't access the endpoint. You might not have the required permissions, or the named credential "namedCredentialNameHere" might not exist. (managedPackageNameSpaceHere)
Is there any way to have managed package code use a subscriber-defined named credential? Perhaps I should update the apex to target the c__ namespace?
Thanks in advance!
UPDATE: Based on comments, I tried 'callout:c__namedCredentialNameHere'. But I received the same result.
The below link mentioned using '.__' as the prefix. I tried that and it failed as well... https://developer.salesforce.com/forums/?id=906F0000000BIAJIA4