How to get the Community URL in LWC?

on

|

views

and

comments

Getting the Salesforce ORG base Url or Community Base URL in Salesforce Lightning is very important as there are various use cases where you need to use Salesforce/Community Url.

Use Cases

Below are some of the use cases when you need to get the org URL

How to get the Community Url in LWC

There are 3 different ways you can get the Community base URL in Lightning Web Component

Method 1 – Use Salesforce Module

The very first step is to use the @salesforce module in LWC. This method is useful and recommended when you know that the component is used only in the Lightning Experience Community.

import the below two properties of the salesforce community inside the Salesforce Lightning Component

import networkId from '@salesforce/community/Id';
import basePath from '@salesforce/community/basePath';

Now use the below code where ever you want the base URL of your salesforce community

const before_ = `${basePath}`.substring(0, `${basePath}`.indexOf('/s')+1);

const communityUrl = `https://${location.host}${before_}`;

Method 2 – Use Network Class

Salesforce provides the network class that provides multiple standard methods for Salesforce Communities and this class is really helpful for getting the community base URL.

Add the below apex method to your LWC Controller class

@AuraEnabled(cacheable=true)
public static String getOrgBaseUrl(){
    String baseUrl = Network.getLoginUrl( Network.getNetworkId() );
    return baseUrl;
}

Use the @wire method to call the apex class and store the URL in the private property of the JavaScript Class.

import { LightningElement, track, wire } from 'lwc';
import getOrgBaseUrl from '@salesforce/apex/NetworkHelperClass.getOrgBaseUrl';
export default class NotificationComponent extends NavigationMixin(LightningElement) {

    communityBaseUrl;

    @wire(getOrgBaseUrl)
    wiredBaseUrl(result) {
        const { error, data } = result;
        if (data) {
            this.communityBaseUrl = `${data}`;
        } else if (error) {
            console.error('Error: ', error);
        }
    }
}

Now use the communityBaseUrl variable anywhere within the JavaScript method where you need to use the base URL of Salesforce Community.

Method 3 – Use JavaScript Object

You can always use a JavaScript location object to get the host while Lightning Web Component is running inside Salesforce Communities.

Below is the code that will give you the Salesforce Community base URL if the community path is the panther

const basePath = 'panther';

const communityUrl = `https://${location.host}$/{basePath}/`

Resources

  1. https://developer.mozilla.org/en-US/docs/Web/API/Location
  2. Network Class in Salesforce
Amit Singh
Amit Singhhttps://www.pantherschools.com/
Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion
Share this

Leave a review

Excellent

SUBSCRIBE-US

Book a 1:1 Call

Must-read

How to start your AI Journey?

Table of Contents Introduction Are you tired of the same old world? Do you dream of painting landscapes with your code, composing symphonies with data, or...

The Secret Weapon: Prompt Engineering: 🪄

Table of Contents Introduction Crafting the perfect prompt is like whispering secret instructions to your AI muse. But there's no one-size-fits-all approach! Exploring different types of...

How to Singup for your own Production org?

Let's see how we can have our salesforce enterprise Salesforce Org for a 30-day Trial and use it as a Production environment. With the...

Recent articles

More like this

LEAVE A REPLY

Please enter your comment!
Please enter your name here

location.host returns undefined in LWC with digital experienceHow to get the Community URL in LWC?
5/5

Stuck in coding limbo?

Our courses unlock your tech potential