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 

I am Sakthivel Madesh, a certified Salesforce Advanced Developer & Administrator working on Salesforce Technology since 2011. I am currently working in Techforce Services as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 15x Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead 5x Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply

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

*