Sunday, January 5, 2020

How to get Salesforce Base URL in Lightning Web Component(lwc)

To get Salesforce base URL use the window.location object can be used to get the current page address (URL).
The window.location.origin property returns the origin URL of the current page.



Ex URL: https://lwcpractice1-dev-ed.lightning.force.com/lightning/r/Account/001B000000vZWOHIA4/view

HTML File
<template>
    <lightning-card title="Salesforce Base URL in Lighting Web Component">
        <div class="slds-text-heading_medium slds-text-align-center">
            SFDC Base URL : <p></p><lightning-formatted-url value={sfdcBaseURL} ></lightning-formatted-url></p>
        </div>
    </lightning-card>
</template>

JS File
import { LightningElement } from 'lwc';

export default class BaseURL extends LightningElement {
    sfdcBaseURL;

    renderedCallback() {
        this.sfdcBaseURL = window.location.origin;
    }
}

Output

2 comments: