I am trying to create a managed package that has a callout to a webhook. Very simple use case, when opportunities or accounts change we want our server to be notified.
I have setup some Apex triggers and an Apex class to make the webhook callout. Part of the Apex class is as follows:
public static void callout(String content) {
if (Test.isRunningTest()) {
Test.setMock(HttpCalloutMock.class, new Webhook());
}
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('callout:my_credential');
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setBody(content);
h.send(req);
}
The named credential "my_credential" exists and simply consists of an external credential that has custom authentication with some headers with values, e.g. "X-My-Key". Very confusingly, it doesn't seem like named credentials can be included in the managed package (since external credentials can't), so part of my planned installation instructions was to have the users manually create the named credentials after install.
This does not work at all - nothing happens. The server never receives any requests, no Apex exception email is generated.
In the scratch org I am using to develop the package, if I modify the class to replace the callout with the hardcoded URL (e.g. req.setEndpoint('https://myserver.com');
), the server starts getting requests when objects change.
I have tried replacing callout:my_credential
with callout:__my_credential
and callout:packagename__my_credential
. Nothing - nothing happens in either the scratch org I am using to make the package, nor in a dev edition org I installed the package to for testing.
What am I doing wrong here?
c__
, on the named credential reference? Also, check the documentation.