function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Dinesh Kumar WickramasingheDinesh Kumar Wickramasinghe 

Get Org URL Via Web Service

I've created a REST web service on Salesforce and exposed it as a public service (Using Sites). Now I am consuming this service from an external web site. I need to get the Salesforce base URL inside this web service code (Apex) to populate a dynamic URL. I used,
String baseUrl = System.URL.getSalesforceBaseUrl().toExternalForm();
When I call the web service from the external site, this returns always the custom URL I added for the site (Web Service), but not the real base URL which I see when I logged in to the Org.

I need to get the actual base URL which I see when I logged in to my Org. Appreciate your support.
 
NagendraNagendra (Salesforce Developers) 
Hi Dinesh,

I think it's better for you to hold the URL for specific Org in a custom setting and change it according to the base of the ORG.

Thanks,
Nagendra
Dinesh Kumar WickramasingheDinesh Kumar Wickramasinghe
Hi Nagendra,

Thanks for the reply. I also thought the same and guggested this option to my client. But he is looking for more dynamic solution. 
Asif Ali MAsif Ali M
If I understand it correctly, you are trying to get the Salesforce instance url dynamically then try the below code
public static String getInstanceUrl() {
        Organization org = [SELECT InstanceName FROM Organization];
        return 'https://' + org.InstanceName + '.salesforce.com/';
    }