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?

  • 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. – Arnold Jr. Jul 18 '17 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 '17 at 9:04

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

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

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

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 {

    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.

  • 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 '17 at 9:03
  • Sorry, your question was not completely clear, nor was your approach. – glls Jul 19 '17 at 12:20

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

let hostname = window.location.hostname;

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.