Sub topic 1
How Lightning Data Service and its cache back uiRecordApi
When you wire getRecord or call updateRecord, your component does not talk to the Salesforce server directly. It talks to Lightning Data Service (LDS), a client-side layer that runs in the browser. This chapter explains what LDS does, how its cache works, and what that means for the code you write in later chapters.
Prerequisites
This chapter assumes you already know the following:
- JavaScript basics, including modules, promises, and
async/await. - The structure of a Lightning web component: an HTML template, a JavaScript class, and a metadata file.
- The
@apidecorator and therecordIdproperty that Lightning record pages pass to a component. - Salesforce objects, fields, and record IDs.
You don't need to know the @wire syntax yet. The next chapter covers it. Here the examples only show where LDS fits.
What Lightning Data Service is
Lightning Data Service is the data layer of the Lightning Experience framework. It sits between your components and the Salesforce server. It has three jobs:
- It loads records, using Salesforce's User Interface API (UI API) under the hood.
- It keeps the records it has loaded in a client-side cache.
- It enforces the running user's permissions, so object access, field-level security, and sharing rules apply to every read and write.
The lightning/uiRecordApi module is the JavaScript entry point to this layer. Its adapters and functions, such as getRecord, updateRecord, and getRecordNotifyChange, all go through LDS.
Base Lightning components such as lightning-record-form, lightning-record-view-form, and lightning-record-edit-form use LDS too. Because everything uses the same layer, a record loaded by one component is available to the others.
Why a shared cache matters
Consider a record page with three components: a header showing the account name, a summary card showing the name and industry, and a form for editing. Without a shared cache, each component would call the server for the same record. That means three round trips, and three copies that can disagree.
With LDS, the components share one cached copy of the record. This gives you several benefits:
- Fewer server requests. If the data is already in the cache, LDS can provide it without another trip.
- Consistency. All components that read the record see the same values.
- Automatic propagation. When a record changes through LDS, every component wired to that record receives the new values.
You get these benefits without writing any cache code. You don't create, read, or invalidate the cache yourself. You use the adapters, and LDS manages the cache.
How a read flows through LDS
The diagram below shows two components wiring the same record, then one of them saving a change.
Follow the steps:
- Component A asks for the account. The cache has nothing, so LDS requests it from the server and stores the result.
- Component B asks for the same account. LDS serves it from the cache.
- Component A calls
updateRecord. LDS sends the change to the server, then updates the cached record with the saved result. - Both components are wired to the record, so both receive new data.
The cache holds what was requested
The cache is not a full copy of the record. LDS caches the field values that components have requested, so what you ask for matters. When you wire getRecord, you name the fields you need, by using either fields or optionalFields. A later chapter covers the difference. For now, remember that your request decides which values come back, and a field you never requested isn't in your wire result.
This also explains why you import field references from @salesforce/schema instead of typing field names as strings. The import gives LDS a precise, checked reference, and it makes the platform aware that your component depends on that field.
Sharing in practice
Here are two small components that read the same record. Neither knows the other exists. They are shown only to illustrate the cache. Chapter details such as $recordId are covered later.
// accountHeader.js
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
export default class AccountHeader extends LightningElement {
@api recordId;
@wire(getRecord, { recordId: '$recordId', fields: [NAME_FIELD] })
account;
get name() {
return getFieldValue(this.account.data, NAME_FIELD);
}
}// accountIndustryBadge.js
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';
export default class AccountIndustryBadge extends LightningElement {
@api recordId;
@wire(getRecord, { recordId: '$recordId', fields: [INDUSTRY_FIELD] })
account;
get industry() {
return getFieldValue(this.account.data, INDUSTRY_FIELD);
}
}Both components wire the same record but ask for different fields. LDS treats them as requests against one cached record. If either component, or any other component on the page, later saves a change to that account through LDS, the cached record changes. Every wire that reads the changed field then provisions new data.
What the cache does on writes
LDS handles writes in a specific way:
updateRecordsends the change to the server. When the server accepts it, the cache is updated and wired components are refreshed.- If the server rejects the change, for example because of a validation rule or missing permission, the promise rejects. The cached values are not changed.
This is why the later chapters treat reading and updating differently. Reading uses wire adapters, which provision data to your component as the cache changes. Updating uses an imperative function that returns a promise. You call it when the user acts, and it returns once the server has answered.
Where the cache can be out of date
LDS only knows about changes that pass through it. Changes made some other way are invisible to the cache. Common examples:
- An Apex method updates the record on the server.
- A flow, a trigger, or another user changes the record.
- A background process changes related field values, such as a roll-up summary or formula field.
In these cases, the cached copy can be stale until LDS refreshes it. For changes you make yourself, such as calling Apex from your component, you tell LDS the record changed by calling getRecordNotifyChange. LDS then re-reads the record and refreshes the wires. Here is a short preview:
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 chapter on refreshing data covers this in full. The key point for now is that the cache is shared and fast, and it needs a notification when changes happen outside LDS.
Permissions come with the cache
Because LDS enforces the running user's access, you don't write permission checks for these adapters. A user who cannot see a field doesn't get its value back. A required field the user cannot read causes the request to fail with an error. An optional field that the user cannot read is left out of the result. Later chapters explain the difference between required and optional fields.
Note that the cache is separate for each browser session. It does not leak data between users, and it is not something you can inspect or edit from your code. Your code interacts with it only through the adapters and functions.
Common mistakes
- Treating wire data as editable. Records provisioned by a wire are read-only. To change a value, call
updateRecordwith a new fields object. Don't modifythis.account.data. - Assuming Apex changes show up automatically. They don't. Call
getRecordNotifyChangeafter the Apex call finishes. - Hard-coding field names as strings. Use
@salesforce/schemaimports, so LDS and the platform can track your dependencies. - Expecting every field to be present. The cache returns what you requested. If you read a field you never asked for, you get
undefined. - Writing your own cache of record data. A second copy in a component property can disagree with LDS. Read from the wire result instead.
Summary of the model
| Concern | Who handles it |
|---|---|
| Loading a record | LDS, through UI API |
| Storing loaded records | The shared client-side cache |
| Keeping components consistent | LDS, by updating wired components |
| Permissions and sharing | LDS, using the running user's access |
| Changes made by Apex or other sources | You, by calling `getRecordNotifyChange` |
Exercise
A record page has two components wired to the same account through getRecord. Component A saves a new name with updateRecord. Separately, Component B calls an Apex method that changes the account's rating. B does nothing after the Apex call. Which change will Component A and B see without extra code, and why?
Show answer
Both components see the new name, because updateRecord goes through Lightning Data Service, which updates the shared cache and refreshes every wire reading that record. The rating change is not seen, because Apex changes the record on the server without passing through LDS, so the cache stays stale. To fix this, Component B should call getRecordNotifyChange([{ recordId }]) after the Apex call finishes.