Sub topic 11
Refreshing data with getRecordNotifyChange
This chapter assumes you can write a Lightning web component that wires getRecord to a property and reads data and error from the result. It also assumes you know async/await and the @api recordId pattern. You do not need to know the other uiRecordApi adapters.
Why a cache can go stale
Lightning Data Service (LDS) keeps a client-side cache of records. When several components on a page wire getRecord for the same record and fields, they share one cache entry. A change made through LDS updates that entry, and every wire reading it receives the new values.
LDS only knows about changes that pass through it. These do not:
- An Apex method that updates, inserts, or deletes a record.
- A flow, trigger, or process that changes the record after a save.
- A callout or other server-side work that modifies data.
After any of these, the cache still holds the old values. Wired components keep showing them until the cache entry expires or the page reloads. getRecordNotifyChange tells LDS that the server copy has changed, so it should refetch.
The function
getRecordNotifyChange is exported from lightning/uiRecordApi. It is not a wire adapter. You call it imperatively.
import { getRecordNotifyChange } from 'lightning/uiRecordApi';
getRecordNotifyChange([{ recordId: '001xx000003DGb2AAG' }]);It takes an array of objects. Each object has a recordId. The function returns nothing, so there is no promise to await.
When you call it, LDS checks which cached records match the IDs. It then refetches those records from the server and provisions the new values to every wire that reads them. Components do not need to know about each other. They only need a wire on the same record.
Worked example: Apex change followed by a notify
This component calls an Apex method that recalculates a score on the account. The method changes the record on the server, so LDS does not see it. The component then notifies LDS.
import { LightningElement, api } from 'lwc';
import { getRecordNotifyChange } from 'lightning/uiRecordApi';
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 await makes sure the Apex call has finished before the notification. If you notify first, LDS can refetch the record before the server has saved the change, and you get the old values again.
Any component on the page that wires getRecord for this account, such as the AccountSummary component from earlier chapters, now shows the new data without a reload.
What the notify does to your wire
When the refetch finishes, the wire provisions a new result object. For a wire on a property, account.data is replaced with the new record. For a wire on a function, your function runs again with the new { data, error } value.
This means a wired function is a good place to react to a refresh. Reuse the pattern from the function-wire chapter. The code below reacts to every provisioned value, including the ones that follow a notify.
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import SCORE_FIELD from '@salesforce/schema/Account.Score__c';
export default class ScoreWatcher extends LightningElement {
@api recordId;
score;
lastError;
@wire(getRecord, { recordId: '$recordId', fields: [SCORE_FIELD] })
wiredAccount({ data, error }) {
if (data) {
this.score = getFieldValue(data, SCORE_FIELD);
this.lastError = undefined;
} else if (error) {
this.lastError = error.body && error.body.message;
this.score = undefined;
}
}
}Score__c is a custom field used for illustration. Replace it with a field that exists in your org.
Notifying several records
The array form lets you refresh many records in one call. This is useful after Apex changes an account and its related contacts.
getRecordNotifyChange([
{ recordId: this.recordId },
{ recordId: this.primaryContactId }
]);Only records that are already in the cache are refetched. If no component has wired a record, there is nothing to refresh, and the call has no visible effect. That is normal and not an error.
When you do not need it
Do not call getRecordNotifyChange after updateRecord. updateRecord goes through LDS, so the cache already holds the new values and wired components update on their own. An extra notify only causes one more server request.
The same applies to the base record form components, such as lightning-record-edit-form. They save through LDS.
| Change made by | Cache updated automatically | Call getRecordNotifyChange |
|---|---|---|
| `updateRecord` | Yes | No |
| `lightning-record-edit-form` | Yes | No |
| Apex method called from the component | No | Yes |
| Flow or trigger side effects after a save | No | Yes, if you need the side effects visible |
| Another user editing the record | No | Only if you have a way to learn about it |
Server-side effects deserve a closer look. Suppose a trigger on Account sets Industry when Name changes. You call updateRecord to change Name. LDS caches the Name you sent. The trigger's change to Industry happens on the server, and LDS may not have it. If the component shows Industry, call getRecordNotifyChange after the updateRecord promise resolves.
async handleSave() {
const fields = {};
fields[ID_FIELD.fieldApiName] = this.recordId;
fields[NAME_FIELD.fieldApiName] = this.newName;
await updateRecord({ fields });
// A trigger may have changed other fields on the server.
getRecordNotifyChange([{ recordId: this.recordId }]);
}Common errors
- Notifying before the server work is done. Always await the Apex promise first.
- Passing a bare ID. The argument is an array of objects,
[{ recordId }], not a string and not an array of strings. - Expecting a return value. The function returns nothing. Watch the wire for the new data instead.
- Notifying a record nobody wires. There is no cache entry to refresh, so nothing happens. If you need the values in a component, add a wire.
- Using it as a default after every save. This adds server requests and hides the fact that LDS already handled the change.
- Forgetting that the notify is not synchronous. The wire result updates after the refetch completes. Code that reads
this.account.dataon the next line still sees the old value.
Related option: refreshApex
If your component reads data through a wired Apex method, the LDS cache is not involved. refreshApex from @salesforce/apex refreshes that wire result instead. The two tools solve the same problem for two different caches. A component can need both when it shows LDS data and Apex data side by side.
Checking your work
To confirm the refresh works in a real org:
- Place an
AccountSummarycomponent and anAccountScorecomponent on the same Account record page. - Click the recalculate button in
AccountScore. - Watch whether the value in
AccountSummarychanges without a page reload. - Remove the
getRecordNotifyChangeline and repeat. The summary should keep the old value.
If the value does not change in step 3, check three things. The Apex method must have saved the change. The summary must request the changed field in fields or optionalFields. Both components must wire the same record ID.
Exercise
An Apex method called from your component updates the Rating field on an account. Another component on the page wires getRecord for the same account and requests Rating. After the Apex call, the second component still shows the old rating. What do you add, and where do you put it?
Show answer
Call getRecordNotifyChange([{ recordId: this.recordId }]) in the component that calls Apex, after awaiting the Apex promise. Apex changed the record outside Lightning Data Service, so the cache is stale. The notify makes LDS refetch the record and provision the new value to the wire in the second component.
Exercise
A developer calls updateRecord to change an account's Name and then always calls getRecordNotifyChange. A reviewer asks whether the notify is needed. Give one case where it is useful and one where it is not.
Show answer
It is not needed when only the fields sent in updateRecord matter, because updateRecord goes through LDS and updates the cache itself. It is useful when a trigger or flow changes other fields on the server during the save and the component displays those fields, because LDS may not have the server-side changes until it refetches.