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 Utilize Salesforce CLI sf (v2)

The Salesforce CLI is not just a tool; it’s the cornerstone of development on the Salesforce Platform. It’s your go-to for building, testing, deploying, and more. As one of the most important development tools in our ecosystem

Save the day of a Developer with Apex Log Analyzer

Table of Contents What is Apex Log Analyzer? Apex Log Analyzer, a tool designed with Salesforce developers in mind, is here to simplify and accelerate your...

Salesforce PodCast

Introduction Hey Everyone, Welcome to my podcast, the first-ever podcast in India for Salesforce professionals. Achievement We are happy to announce that we have been selected as Top...

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