28

To access chatter rest api using APEX code, I need salesforce instance server url in following format:

https://na17.salesforce.com

Is there any method which works in all cases

Case 1: Any Development Org

Case 2: My Domain Enabled

Case 3: Sandbox

Case 4: Managed Package

41

You are looking for URL.getSalesforceBaseUrl().

Edit

The Winter 19 release is introducing another method to get the URL, which will give you the canonical URL for your org: URL.getOrgDomainUrl().

Returns the canonical URL for your org. For example, https://yourDomain.my.salesforce.com or, for orgs without My Domain enabled, https://yourInstance.salesforce.com.


Both of the above methods return a System.URL. If you would like the string version, simply call .toExternalForm() on that instance. For example:

String baseUrl = URL.getSalesforceBaseUrl().toExternalForm();
  • 9
    System.Url.getSalesforceBaseURL().toExternalForm() to be more precise. – Ashish Narang Jan 29 '15 at 3:05
  • 4
    Any idea how to get your instance if you have mydomain enabled ? – Samuel De Rycke Jul 1 '16 at 12:41
  • 1
    @SamuelDeRycke You need to use the status/trust API. See api.status.salesforce.com/v1/docs/#!/instances/… – Daniel Ballinger Feb 6 '17 at 19:50
  • @SamuelDeRycke Didn't see that you had pinged this answer until just now. – Adrian Larson Feb 6 '17 at 19:51
  • 2
    @SamuelDeRycke Salesforce is now adding new method in Winter19 i.e. System.Url.getOrgDomainUrl() – Ashish Narang Aug 25 '18 at 4:50
20

To retrieve a URL object containing the full beginning portion of the URL, such as "https://na17.salesforce.com", you may use the static method:

    System.URL.getSalesforceBaseURL();  // Yields URL object for https://xxxx.salesforce.com

As Ashish notes above, suffixing this method with ".toExternalForm()" will convert the URL object into its text representation, as follows:

    System.URL.getSalesforceBaseURL().toExternalForm();  // Yields string "https://xxxx.salesforce.com"

Furthermore, if you wish to retrieve only the subdomain portion of the URL (the "na17" part to the left of the ".salesforce.com" domain), use:

    System.URL.getSalesforceBaseURL().getHost();  // Yields subdomain (host) as string

These methods will work reliably across Production, Sandboxes, Dev Orgs, etc. Which one you choose depends upon your specific need in the code that you are writing.

Lastly, if you're trying to create a host-independent object link, you may do so as follows:

    String link = '<a href="' + System.URL.getSalesforceBaseUrl().toExternalForm() + '/' + sObject.Id + '" target="_blank">sObject Link</a>';
  • 4
    Any idea how to get your instance if you have mydomain enabled ? – Samuel De Rycke Jul 1 '16 at 12:42
2

I used this approach to get just the instance, as in managed packages the other one approaching is not working for me

String urlInstance = String.valueof(System.URL.getSalesforceBaseURL()).replace('Url:[delegate=','').replace(']','');
String[] instance = urlInstance.split('\\.');
req.setEndpoint('https://'+instance[1]+'.salesforce.com/services/apexrest/cloudmobile/myservice/');

Hope it helps.

0

You can get the instance name from the Organization object using:

select InstanceName from Organization

Unlike the solutions using 'System.URL.getSalesforceBaseURL()...' this will work for you in:

Case 1: Any Development Org Case 2: My Domain Enabled Case 3: Sandbox Case 4: Managed Package

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.