5

I have a managed app that uses URL.getSalesforceBaseUrl().toExternalForm() to create a Remote site for ToolingApi. This works great in all environments (Production, Dev/scratch org), however, it's pretty unreliable in Sandboxes. Sometimes it returns the proper URL sometimes it doesn't.

For example: While running the app in a sandbox, this line of code

URL.getSalesforceBaseUrl().toExternalForm() returns https://examplesite--testing.my.salesforce.com

However, using that same method inside Execute anonymous returns https://exmplesite--testing.cs61.my.salesforce.com which is the correct url needed for the toolingApi.

Anyone have any insight on what could be happening?

8

In Winter 19, we will have a new method, System.Url.getOrgDomainUrl(), which will always return the correct URL.

Use the new System.Url.getOrgDomainUrl() method to interact with Salesforce REST and SOAP APIs in Apex code. Get endpoints for User Interface API calls, for creating and customizing picklist value sets and custom fields, and more. Use the updated System.UserInfo.getSessionId() method to retrieve session IDs, even when your code runs asynchronously.

The System.URL.getSalesforceBaseUrl() method is intended to return the host for the current context (e.g. Lightning, Visualforce, Classic UI, Communities). This new method will always return the correct My Domain URL that should be used, and should work correctly with all Sandboxes.

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.