We have a strong appreciation for the Lightning UI and the aura feature (e.force:refreshView) that efficiently refreshed data on the current Lightning page, record, or component. We found this functionality lacking in LWC, and observed that many resorted to a workaround involving a combination of Aura and LWC to achieve this.
However, with the advent of Winter ’21, a new solution has emerged!
Using it is as simple as what we’ve been used to in Aura. You’ll just need to import it
(import { getRecordNotifyChange } from 'lightning/uiRecordApi';
), and then call it with a list of record Ids
(getRecordNotifyChange([{recordId: this.recordId}]);
).
Full 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: ... }) record; 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 a notable distinction: force:refreshView used to refresh the entire page, whereas getRecordNotifyChange() exclusively refreshes the records you specify. Therefore, if your Apex code, for example, updates the current Account along with some associated child Contacts, you may need to provide getRecordNotifyChange() with all the IDs that were modified by your Apex method.