How to Capture Salesforce Base URL Without using Apex in Aura Component or Lightning Web Component?

This blog post describes about to get the Salesforce Base URL without using URL Apex Class.

As we know its possible to capture the salesforce instance URL using URL Class – https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_url.htm

From this approach, we can get the url without using apex class or design attributies or custom label/custom settings

get community base url in my lightning component without apex in Aura Component or Lightning Web Component:
As you know the community url always with Salesforce Base url with append on “/s” so if you like to get the salesforce instance url (base url) then use with

let urlString = window.location.href;
let baseURL = urlString.substring(0, urlString.indexOf(“/s”));
console.log(baseURL);

Example:
https://sakthivel-developer-edition.ap15.force.com/CapricornCafe/s/

Output:
https://sakthivel-developer-edition.ap15.force.com/CapricornCafe

get salesforce base url in my lightning component without apex in Aura Component or Lightning Web Component:
if you are in account detail page and to get the base url.

let urlString = window.location.href;
let baseURL = urlString.substring(0, urlString.indexOf(“/s”));
console.log(baseURL);

Example:
https://sakthivel-developer-edition.lightning.force.com/lightning/r/Account/00190000006AqEUAA0/view

Output:
https://sakthivel-developer-edition.lightning.force.com

 

Written by 

Salesforce MVP | Lightning Champion | 16x Salesforce Certified | Application Architect | 6x Trailhead Ranger | Techforce Services | Australia

Leave a Reply

Your email address will not be published. Required fields are marked *

*