3

I've created a managed package to deploy some custom fields etc. I need a post install script to run and notify my server than the salesforce instance has installed the package. Here is my post install script:

public class PackagedInstallScript implements InstallHandler {
    public void onInstall(InstallContext context) {
        String payload = getPayload(context);
        sendNotification(payload);
    }
    
    private String getPayload(InstallContext context) {
        String version;
        if (Test.isRunningTest())
            version = '1.0';
        else
            version = System.requestVersion().major() + '.' + System.requestVersion().minor();
        
        JSONGenerator gen = JSON.createGenerator(true);
        gen.writeStartObject();
        gen.writeStringField('Action', 'INSTALL_PACKAGE');
        gen.writeStringField('InstanceUrl', URL.getSalesforceBaseUrl().toString());
        gen.writeStringField('version', version);
        gen.writeEndObject();
        return gen.getAsString();
    }
    
    @future(callout=true)
    public static void sendNotification(String payload) {
        HttpRequest request = new HttpRequest();
        request.setEndpoint('callout:my_external_app');
        request.setMethod('POST');
        request.setBody(payload);
        
        if (!Test.isRunningTest())
            (new Http()).send(request);
    }
}

When I install the package on another salesforce instance, I expect to receive a notification in my external application, however I get nothing. I'm not sure where I can find the debug log for this script to see if it's thrown an error?

Also note I am referencing a named credential for the url for my external app which is part of the managed package. I wonder if my namespace prefix affects how I reference it?

What am I doing wrong?

4

Yes, you need to use the namespace (see this question, basically, callout:myNameSpace__myCalloutName).

4
  • OK so adding my namespace prefix to the callout as suggested worked. WITHOUT the namespace prefix I was getting an 'unauthorized endpoint' exception but I'm still able to do without remote site settings by using the namespace prefix. Thanks. – rosghub Aug 9 '20 at 0:49
  • @jefferson Ah, good to know. I'll amend. Glad I was able to help! – sfdcfox Aug 9 '20 at 1:26
  • @jefferson Just confirming. You are able to make a callout to an external endpoint with this? It is a bit concerning that the remote site setting isn't required. – Daniel Ballinger Aug 9 '20 at 19:31
  • Yes. If you remove the managed package from the situation, I'm able to reference/make callouts to named credentials without remote site settings. When distributing in a managed package, like I said the namespace prefix is required otherwise you get an unauthorized endpoint exception even though the callout:named_cred resolves to the endpoint correctly. Odd behavior but it is what it is. – rosghub Aug 9 '20 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.