[SalesForce] Get Org URL from Lightning component

I am operating in an org: https://myorg.lightning.force.com. If I call URL.getSalesforceBaseUrl() from an apex class I get the base org URL back. If I then use the same command in a controller for a lightning component in a community I get the community's base URL: https://mycommunity.aa11.force.com.

Any ideas on how I can get the Org URL (https://myorg.lightning.force.com) in a lightning component dynamically?

Best Answer

Sorry for reviving an old post, but I was trying to find the same answer as OP and wanted to provide my solution for anyone else who might stumble upon this post:

   public static String getLexOriginUrl() {
        String prefix = URL.getSalesforceBaseURL().toExternalForm().replace('https://', '').split('\\.')[0];
        prefix = prefix.removeEnd('--c');
        return 'https://' + prefix.toLowerCase() + '.lightning.force.com';
Related Topic