Sub topic 13
Keeping the rest of the page in sync after a save
This chapter assumes you can build a form with lightning-record-form or lightning-record-edit-form and handle its onsuccess event. It also assumes you know how @wire works. Here you will see which parts of a page update on their own after a save and which parts you must refresh yourself.
What refreshes automatically
Both form components save through Lightning Data Service (LDS). LDS keeps a client-side cache of records. After a successful save, it updates that cache and notifies every component that reads the same record through LDS.
These consumers update without any code from you:
- Other
lightning-record-form,lightning-record-edit-formandlightning-record-view-formcomponents showing the same record. - Components using
@wire(getRecord, ...). - Standard record page components that read through LDS.
What does not refresh automatically
Data fetched through Apex is outside the LDS cache. A wired Apex method, or an imperative Apex call, keeps its old result after the save. A related list built from Apex will show stale values until you refresh it.
| Data source | Updates after a form save? | How to refresh |
|---|---|---|
| `getRecord` wire | Yes | Nothing to do |
| Other record forms on the same record | Yes | Nothing to do |
| `@wire` Apex method | No | `refreshApex` |
| Imperative Apex call | No | Call the method again |
| Record changed by a trigger or Apex save | LDS cache may be stale | `notifyRecordUpdateAvailable` |
Refreshing a wired Apex result
Keep the whole object that the wire provisions, not just data. Pass that object to refreshApex in the success handler.
import { LightningElement, api, wire } from 'lwc';
import { refreshApex } from '@salesforce/apex';
import getContacts from '@salesforce/apex/ContactController.getContacts';
export default class ContactPanel extends LightningElement {
@api recordId;
wiredContacts;
contacts;
@wire(getContacts, { accountId: '$recordId' })
wiredResult(result) {
this.wiredContacts = result;
if (result.data) {
this.contacts = result.data;
}
}
handleSuccess() {
refreshApex(this.wiredContacts);
}
}In the template, add onsuccess={handleSuccess} to the form. The Apex method must be annotated @AuraEnabled(cacheable=true) to be used with @wire.
Telling LDS that a record changed elsewhere
Sometimes the server changes a record in a way LDS did not see. An Apex save, a flow or a trigger that updates related fields are examples. The LDS cache then holds old values. Call notifyRecordUpdateAvailable with the affected record IDs. LDS then re-reads those records and updates the components that use them.
import { notifyRecordUpdateAvailable } from 'lightning/uiRecordApi';
async handleSuccess(event) {
await notifyRecordUpdateAvailable([{ recordId: event.detail.id }]);
}The function returns a promise. Await it if later code depends on the refreshed data.
Common mistakes
- Calling
refreshApexwithdatainstead of the full wire result. The call does nothing useful. - Expecting an Apex-driven list to update because a form on the same page saved.
- Refreshing in
onsubmit, before the server has saved the record. - Forgetting
cacheable=true, which makes the Apex method unusable with@wire.
Exercise
A page has a lightning-record-edit-form for an Account and a custom component that lists the Account's Contacts using a wired Apex method. After saving the form, the Contact list still shows old data. What do you change, and why?
Show answer
Store the full result object that the wire provisions (not only its data), and call refreshApex on it from the form's onsuccess handler. The Apex result is not in the LDS cache, so the save does not refresh it on its own. onsuccess is the right place because the save has finished by then.