7

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?

2
  • 1
    We can always use Custom Metadata Types to store a static data and use across the org. For more information, please refer to: Link. You just need to query it in your Apex controller class, and refer in the Lightning's JS controller class.
    – SatyaV
    Jul 18, 2017 at 19:53
  • 1
    This is what I've done for now, created a custom setting and retrieved it in the apex, not ideal as it adds another step for setting up a new org but the only way I could solve it for now.
    – Simon
    Jul 19, 2017 at 9:04

4 Answers 4

3

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';
    }
2

String Host = URL.getSalesforceBaseUrl().toExternalForm() + '/';

This will return https://yourinstance.salesforce.com/

then you can pass this to your controller.js file and use it. Hope this answer your question

3
  • This doesn't work as the comments on glls answer say.
    – Simon
    Dec 14, 2017 at 13:09
  • yes in that case it might not.. :) Dec 15, 2017 at 6:23
  • its better to raise a case in salesforce support and ask them only. Dec 15, 2017 at 6:25
1

You can simply reference the apex class as your server side controller and fetch the value on the client side?

Server side controller:

public with sharing class theorgurl {

    @AuraEnabled
    public static String fetchit(){
        string urler = URL.getSalesforceBaseUrl().toString();
        return (urler);
    }
}

Client side controller:

"geturl" : function(cmp) {

        var action = cmp.get("c.fetchit");
        action.setParams({ urlname : cmp.get("v.urlname") });

        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
             ...Do something with response.getReturnValue()...
        }
}

If you are unsure on how to proceed, I would recommend you go through the Lightning Components Developer Guide since

the framework supports client-side and server-side controllers.

2
  • 1
    This is exactly what I said didn't work, the URL.getSalesforceBaseURL() when called from a community context returns the base URL for the community, not the main org.
    – Simon
    Jul 19, 2017 at 9:03
  • Sorry, your question was not completely clear, nor was your approach.
    – glls
    Jul 19, 2017 at 12:20
1

In case you want to get your org URL without APEX, use:

let hostname = window.location.hostname;
2
  • 1
    I think this is the only simple and efficient way to find the lightning base URL
    – Patlatus
    Nov 10, 2021 at 11:05
  • This may have worked in the past, but today in LWC window.location.hostname returns undefined. Security has been tightened again. Stop having fun. 🤪 Aug 17, 2022 at 6:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .