August 19, 2021

How to refresh cache when using Lightning Navigation in Lightning Web Component in Salesforce?

Please check the following article.
https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.use_navigate_basic

[NavigationMixin.GenerateUrl](pageReference)
A component calls this[NavigationMixin.GenerateUrl] to get a promise that resolves to the resulting URL. The component can use the URL in the href attribute of an anchor. It can also use the URL to open a new window using the window.open(url) browser API.


Sample Code:
HTML:

<template>    
    <lightning-button variant="brand" label="Cancel" onclick={cancel}></lightning-button>
</template>

JavaScript:
import { LightningElement, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';

export default class NewEmployeeChild extends  NavigationMixin( LightningElement ) {

    @api accountId;
    accountURL;
    url;

    connectedCallback() {
        
        this.accountURL = {
            type: 'standard__recordPage',
            attributes: {
                recordId: this.accountId,
                objectApiName: 'Account',
                actionName: 'view'
            }
        };
        this[ NavigationMixin.GenerateUrl ]( this.accountURL )
            .then( url => this.url = url );

    }

    cancel() {

        console.log( 'URL is ' + this.url );
        window.open( this.url, '_self' );

    }

}

No comments:

Post a Comment