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

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

NavigationMixin.GenerateUrl can be used to generate URL and window.open can be use to open the generated URL o refresh cache when using Lightning Navigation in Lightning Web Component in Salesforce.

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' );

    }

}

Leave a Reply