12

I have created a managed package that uses tooling API for updating class, trigger etc. Everything runs fine with all sorts of testing. I have created this package in my Developer org. I had to set the endpoint for Tooling API.

Added the remote Site setting to package too. It failed to work in one of our sandbox since the endpoint was different there (Which i realized later). After a long research I got to know may be with Metadata API I will create Remote Site URL with Apex. But again that requires endpoint.

Is there any way I can over come this issue ?

2
  • What is the actual issue you are having, that you need to change the API endpoint between sandbox and production? If so, that is unfortunately going to be the case. Jan 24, 2014 at 5:15
  • I have created a managed package which uses Tooling API . It requires endpoint to be set in RemoteSite settings. So it was different for different instances which needed to happen dynamicaly after installing package.
    – Soberano
    Feb 4, 2014 at 8:58

2 Answers 2

9

The "correct answer" is that administrators are expected to configure any endpoints that they need to use manually in a situation like this. This is a one time process, takes only a minute to complete, and is often used in packages that access salesforce.com servers. This is the best solution, because with the addition of "my domain" settings, there are nearly unlimited possible salesforce.com URLs, and you can't use wild cards.

That said, if you wanted to automate it, you could:

  • Write a Flash script that performs the call, and embed it in a Visualforce page.
  • Write a Java applet that performs the call, and embed it in a Visualforce page.
  • Write a third-party server with known endpoint (embedded in the package), and callout to that known endpoint to perform the call.
  • Write some logic in JavaScript, and call that from a Visualforce page.
  • Write a Silverlight app that performs the call, and embed it in a Visualforce page.

As you can see, most solutions are probably over the top. Just have the administrator enable it once and forget about it.

In Apex Code, you can determine the correct endpoint by using Url.getSalesforceBaseUrl() to make the Apex Code part dynamic.

1

If you need to do this automatically in your package, I recommend creating a post install script and using the Metadata API to accomplish this. See here for how to create a post install script.

Below is code I copied over from this answer for creating Remote Site Settings from Apex (answer is mine on StackOverflow). See the original answer for more details and dependencies.

future(callout=true)
public static void createRemoteSite(String fullName, String url) {
    //Metadata Service
    MetadataService.MetadataPort service = new MetadataService.MetadataPort();
    service.SessionHeader = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId = UserInfo.getSessionId();

    //Metadata for remote site settings
    MetadataService.RemoteSiteSetting remoteSiteSettings = new MetadataService.RemoteSiteSetting();
    remoteSiteSettings.fullName = fullName;
    remoteSiteSettings.url = url;
    remoteSiteSettings.description = 'Remote site settings created from apex';
    remoteSiteSettings.isActive = true;
    remoteSiteSettings.disableProtocolSecurity = false;
    MetadataService.SaveResult[] results = service.createMetadata(
      new List<MetadataService.Metadata>{ remoteSiteSettings }
    );

    for (MetadataService.SaveResult result : results) {
      if (result.success) {
        //Success
        System.debug(result);
      } else {
        //Failed
        System.debug(result.errors[0].message);
      }
    }
  }
1
  • You may also do an upsertMetadata on the records. The unique key is fullName.
    – Shanerk
    Nov 16, 2020 at 22:15

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.