Sub topic 1
How Lightning Data Service and Its Cache Back `uiRecordApi`
Prerequisites
Assume you know JavaScript modules, promises, async/await, and the structure of a Lightning web component (LWC). You should also know that recordId receives the current record’s ID on a record page.
Lightning Data Service and the Shared Cache
Lightning Data Service (LDS) reads and updates Salesforce records for components. It keeps a client-side cache so components can reuse data instead of making repeated server requests. LDS also coordinates changes across components that use the same record.
The lightning/uiRecordApi module provides functions that use LDS. Reading normally uses a wire adapter. Updating uses an imperative function, such as updateRecord.
Wire Adapters and `@wire`
A wire adapter is a framework-provided data source. The @wire decorator connects that source to a property or function. The framework provisions a result when its parameters are available or change.
Importing Schema References
Import object and field references from @salesforce/schema instead of hard-coding API names. These references allow tooling to validate dependencies and help Salesforce track fields used by the component.
Reading a Record with `getRecord`
getRecord is the wire adapter for retrieving one record. Its fields option lists fields that the component requires.
Making the Wire Reactive with `$recordId`
Prefix a parameter with $ to make it reactive. Here, '$recordId' means the adapter runs again when the component’s recordId value changes.
import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
export default class AccountSummary extends LightningElement {
@api recordId;
@wire(getRecord, {
recordId: '$recordId',
fields: [NAME_FIELD]
})
account;
}The wire result is stored in account. Later, the template or JavaScript can use account.data after a successful response and account.error after a failed response.
Exercise
Why should a component import Account.Name instead of placing 'Name' directly in the fields array?
Show answer
The schema import provides a validated field reference and avoids hard-coding the field’s API name.