I am calling a external web service via APEX and it is working fine..However the endpoint(URL) is hard coded in the code and it would be different when I migrate the code to QA sandbox..How can customize the URL? I am thinking of using Custom Settings approach and store the URL as custom setting. Is this the right approach? would appreciate any help.



I used custom settings for this, and it works well. I store the host name separate from the paths. This has the additional advantage that you can switch to endpoints on different environments fast.

Edit to add this example:

I use a ServiceCenterConfig__c list custom setting with two fields (Name, Value__c) to store general configuration information for an application. One record is [name="msc server", value__c="https://production.server.com"], another is [name="msc path", value__c="/path/to/rest/endpoint"]. In a sandbox, I can set "msc server" to another value, e. g. "https://staging.server.com". The "msc path" record stays the same for all environments.

This way, I can connect a sandbox to the same web service on a different environment than my production org. You could connect your UAT or integration sandbox to a staging or integration environment of the web service.

But since it's Summer '17 already, I would recommend to look into custom metadata instead of custom settings to do this.

  • i'd love an example of this
    – Jay Rizzi
    Jun 12 '17 at 18:44
  • I have edited my answer.
    – georg w.
    Jun 13 '17 at 8:32
  • Thanks for the update...any resources on how one would accomplish this using custom metadata? As the only problem the original answer has i copying prod down to a sandbox will constantly have people updating the server address
    – Jay Rizzi
    Jun 13 '17 at 12:39
  • 1
    Using custom metadata would look very much alike. Just substitute the list custom setting with a custom metadata type. Yes, updating the server address is necessary after a sandbox refresh. But you can automate this with a SandboxPostCopy class: developer.salesforce.com/docs/atlas.en-us.apexcode.meta/….
    – georg w.
    Jun 14 '17 at 7:37

I concur with above. I use custom settings to stash the URL and associated environment (Stage/Prod), then I use the following code (originally found on SE) to detect the environment executing the code:

public static Boolean isSandboxInstance()
    String s  =  System.URL.getSalesforceBaseUrl().getHost();
    return (Pattern.matches('(.*\\.)?cs[0-9]*(-api)?\\..*force.com',s)) ? true : false;

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.