Sub topic 11
Refreshing data with getRecordNotifyChange
This chapter assumes you can wire getRecord to a component, read data and error from the result, and call updateRecord. It also assumes you know async/await and how recordId reaches a record page component.
Why a wire can show stale data
Lightning Data Service (LDS) keeps a client-side cache of records. Every component that wires getRecord for the same record reads from that cache. When you save with updateRecord, LDS writes the new values to the cache, and all wires on that record receive them.
LDS only knows about changes it handled itself. If something else changes the record, the cache still holds the old values. Common causes are:
- An Apex method that updates the record.
- A flow or a trigger that runs on the server.
- A call to a REST endpoint.
The wire keeps showing the old values until you tell LDS the cached record is out of date.
What getRecordNotifyChange does
getRecordNotifyChange is a function exported by lightning/uiRecordApi. It is not a wire adapter. You call it imperatively, and you pass it an array of objects that each have a recordId.
LDS marks those records as stale in the cache. It then fetches fresh data from the server. Every wire that reads one of those records gets a new result.
Example: refresh after an Apex call
The component below calls an Apex method that recalculates a score on the account. Apex changes the record on the server, so the component must notify LDS afterward.
import { getRecordNotifyChange } from 'lightning/uiRecordApi';
import { LightningElement, api } from 'lwc';
import recalculateScore from '@salesforce/apex/AccountController.recalculateScore';
export default class AccountScore extends LightningElement {
@api recordId;
async handleRecalculate() {
await recalculateScore({ accountId: this.recordId });
getRecordNotifyChange([{ recordId: this.recordId }]);
}
}The order matters. The call to getRecordNotifyChange comes after await, so the server change has finished before LDS asks for fresh data. If you call it before the Apex promise resolves, LDS may fetch the old values and the wire will still be stale.
You can pass more than one record. Each entry in the array refreshes one record:
getRecordNotifyChange([
{ recordId: this.accountId },
{ recordId: this.contactId }
]);When you do not need it
- After
updateRecord. LDS already updated the cache. - After a base record form component, such as
lightning-record-edit-form, saves a record. LDS handles that save too. - When the component uses an imperative Apex call to read data, rather than a wire on
getRecord. Nothing is cached, so nothing needs notifying.
Common mistakes
- Calling
getRecordNotifyChangewithout waiting for the server call to finish. - Passing a single object instead of an array.
- Expecting it to refresh a wire on an Apex method. It refreshes LDS records only. Wires on Apex methods need
refreshApex. - Passing a record ID for a record no component has loaded. This does no harm, but it has no visible effect.
Exercises
Exercise
A component wires getRecord for an Account. A button calls an Apex method that changes the Account's Rating field on the server. After the click, the page still shows the old Rating. What one line should you add, and where?
Show answer
Add getRecordNotifyChange([{ recordId: this.recordId }]); after the awaited Apex call in the click handler. Apex changed the record outside LDS, so the cache is stale until you notify LDS. Once the call is made after the Apex promise resolves, LDS refetches the record and the wire emits the new Rating.
Exercise
Why is getRecordNotifyChange unnecessary after a successful updateRecord call?
Show answer
updateRecord goes through LDS, so LDS writes the new values to its cache and every wire that reads the record receives them automatically.