html code

Use force:refreshView (or getRecordNotifyChange) in a Lightning web component.

We have a strong appreciation for the Lightning UI and the aura feature (e.force:refreshview), which updates the data on the current Lightning page, record, or component. However, we found that this feature was missing in LWC, and many resorted to using a combination of Aura and LWC to achieve this functionality.

As of Winter ’21, there’s a new player in town!

Utilizing it is as straightforward as what we’re accustomed to in Aura. Simply import it ( import { getRecordNotifyChange } from 'lightning/uiRecordApi';), and then invoke it with a list of record Ids (getRecordNotifyChange([{recordId: this.recordId}]);).

The complete sample code looks like this:

import { LightningElement, wire } from 'lwc';
import { getRecord, getRecordNotifyChange } from 'lightning/uiRecordApi';
import apexUpdateRecord from '@salesforce/apex/Controller.apexUpdateRecord';
export default class NotifyRecordChangeExample extends LightningElement {
    @api recordId;
    // Wire a record.
    @wire(getRecord, { recordId: '$recordId', fields: ... })
    async handler() {
      // Update the record via Apex.
      await apexUpdateRecord(this.recordId);
      // Notify LDS that you've changed the record outside its mechanisms.
      getRecordNotifyChange([{recordId: this.recordId}]);

This does introduce one notable change: while force:refreshView used to refresh the entire page, getRecordNotifyChange() now specifically refreshes the records you specify. Therefore, if your Apex code updates not only the current Account but also some child Contacts, you might need to provide getRecordNotifyChange() with all the IDs updated by your Apex method.

Credit goes to: