I have some customer orgs that still work on Classic and didn't enable MyDomain. This causes strange errors. Is there a way I can check with Apex if an org has a MyDomain enabled?


I would just check:

Boolean isMyDomainEnabled = URL.getSalesforceBaseUrl().getHost().endsWith('my.salesforce.com');

Or maybe:

Boolean isMyDomainEnabled = URL.getSalesforceBaseUrl().getHost().contains('--');

That should return true for all orgs with MyDomain enabled, as the url format there would be domain--sandbox.cs42.my.salesforce.com. In orgs without, it would just be cs42.salesforce.com.

| improve this answer | |
  • I have orgs without "my" in their mydomain. – Robert Sösemann Feb 19 '18 at 15:20
  • 1
    What about checking .contains('--')? – Adrian Larson Feb 19 '18 at 15:22

Try URL.getSalesforceBaseUrl().getHost();. In some cases you might also get useful info out of a query such as [SELECT fullPhotoUrl FROM User WHERE userType = 'Standard' LIMIT 1].

Credit to Cristophe Coenrats for both. His repo here https://github.com/ccoenraets/lightning-component-apex-continuation contains code that allows Apex continuations in Lightning, and as a result his Visualforce and Lightning components need to successfully guess each other's base URLs in order to communicate successfully.

| improve this answer | |

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.