Sub topic 12
Refreshing record data after changes
This chapter assumes you can build a screen action that saves with updateRecord and closes with CloseActionScreenEvent, as shown in earlier chapters. It also assumes you know what an imperative Apex call is. Here you will learn why the record page sometimes shows stale values after an action runs, and how to fix it.
Why a page can look stale
Lightning Data Service (LDS) keeps a client-side cache of record data. Components that read through LDS, such as lightning-record-view-form and the getRecord wire adapter, all read from that cache.
updateRecordwrites through LDS. The cache is updated, and every component that reads the record re-renders. You need no extra code.- Apex changes are invisible to LDS. If your action calls an Apex method that changes the record, the cache still holds the old values. The page keeps showing them until you tell LDS to re-fetch.
The same applies to other data on the page, such as related lists. A change to a child record does not update the parent's cache entry.
Refreshing one record with notifyRecordUpdateAvailable
notifyRecordUpdateAvailable tells LDS that the listed records changed on the server. LDS then re-fetches them and updates every component that uses them. It returns a Promise.
import { LightningElement, api } from 'lwc';
import { CloseActionScreenEvent } from 'lightning/actions';
import { notifyRecordUpdateAvailable } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import recalculate from '@salesforce/apex/AccountController.recalculate';
export default class RecalculateAccount extends LightningElement {
@api recordId;
async handleRun() {
try {
await recalculate({ accountId: this.recordId });
await notifyRecordUpdateAvailable([{ recordId: this.recordId }]);
this.dispatchEvent(new ShowToastEvent({
title: 'Success',
message: 'Account recalculated',
variant: 'success'
}));
this.dispatchEvent(new CloseActionScreenEvent());
} catch (error) {
this.dispatchEvent(new ShowToastEvent({
title: 'Error',
message: error?.body?.message || 'Unknown error',
variant: 'error'
}));
}
}
}The order matters. Await the Apex call first, so the server holds the new data. Then notify LDS, and only then close the panel.
Refreshing the whole page with RefreshEvent
When the action changes data that LDS does not track for this record, ask the page to refresh itself. Import RefreshEvent from lightning/refresh and dispatch it.
import { RefreshEvent } from 'lightning/refresh';
// after the server work succeeds
this.dispatchEvent(new RefreshEvent());Components on the page react to this event. Standard components refresh by themselves. A custom component that needs to refresh must register a handler for it. A component can listen with registerRefreshHandler from lightning/refresh.
Common mistakes
- Calling
notifyRecordUpdateAvailablewithout awaiting the Apex call. LDS may re-fetch before the change is committed and cache the old values again. - Passing the wrong shape. The argument is an array of objects,
[{ recordId }], not a bare ID string. - Adding refresh code after
updateRecord. It is redundant, because LDS already updated the cache. - Dispatching
CloseActionScreenEventfirst and refreshing afterwards. Do the refresh first, so the user never sees the old values behind the closing panel.
Exercise
An action calls an Apex method that updates the Account's Description field. After the panel closes, the Description on the record page is still the old value. What is the cause, and what one line fixes it?
Show answer
Apex changed the record outside Lightning Data Service, so its cache is stale. After awaiting the Apex call, add await notifyRecordUpdateAvailable([{ recordId: this.recordId }]);, imported from lightning/uiRecordApi, before closing the panel.
Exercise
When is RefreshEvent a better choice than notifyRecordUpdateAvailable?
Show answer
Use RefreshEvent when the action changes data beyond one record's cached fields, such as related lists or other components on the page. Then a full page refresh is needed.