Applying LDS Best Practices
Apply the Lightning Data Service guidelines to a Lightning Web Component. Three pillars: data consistency, referential integrity, and UIAPI vs Apex. Focused on the UI API path — GraphQL and upstream data-requirements analysis are handled out-of-band.
When to Use
- Reviewing a component's data layer for LDS compliance (hand-rolled forms, stringly-typed field names, un-synchronized Apex + LDS, Apex overuse).
- Implementing CRUD on standard or custom objects.
- Deciding between
getRecord, getRecords, createRecord, updateRecord, deleteRecord, base record form components, or Apex.
- Fixing stale-data bugs after record mutation.
- Adding schema imports (
@salesforce/schema/...) to replace hard-coded field/object names.
Do NOT use this skill for:
- GraphQL query/mutation generation — handled out-of-band today.
- Upstream data-requirements discovery — handled out-of-band today.
- SLDS class / design-token work (use
design-systems-slds-apply).
- Accessibility, security, or RTL review — those are separate passes run with their own tooling.
Prerequisites
- Component path.
- Understanding of the component's data operations (read / write / both) and whether Apex is already involved.
- Access to the org's schema for
@salesforce/schema imports (Setup → Object Manager → <Object> → Details → API Name; or, when GraphQL serves the read, an SDL pulled from the target org).
Knowledge Bases
- references/lds-expert.md — authoritative LDS knowledge (patterns, adapters, caching, mutation flows).
- references/lds-data-consistency.md — cache invalidation,
refreshApex, notifyRecordUpdateAvailable, wire result propagation.
- references/lds-referential-integrity.md —
@salesforce/schema imports, field constants, object-name resolution, and their propagation through refactors.
Per-adapter API reference — references/adapter-apis.md holds Syntax / Parameters / Returns / Usage for every UI API adapter, grouped by family (uiRecordApis, uiListsApis, uiRelatedListApis, uiObjectInfoApis). Each adapter is a # `<name>` block; grep for the backticked name (e.g. # `getRecord`) to jump to its entry. Read this before wiring an adapter; do not paraphrase from memory.
Type catalog — references/wire-adapter-types.md holds every type the adapters return (Record, ObjectInfo, FieldValue, etc.), grouped by category and rendered with the same formatter the legacy MCP tool used. Grep for ## <TypeName> to jump to a specific entry.
Read the applicable reference before editing code.
Core Principles
- Prefer LDS/UIAPI for CRUD on standard and custom objects. Use Apex only when business logic or bulk operations exceed LDS capabilities.
- Always keep rendered data fresh with
refreshApex(wiredResult) or notifyRecordUpdateAvailable([{ recordId }]) after any mutation.
- Import object and field references from
@salesforce/schema — not string literals. This protects the component against metadata renames.
- Favor base record form components (
lightning-record-form, lightning-record-edit-form, lightning-record-view-form) for single-record UIs. They ship with validation, SLDS styling, accessibility, and field-level security.
Review Checklist
Answer Yes / No to each. Any Yes triggers a refactor.
1. Hand-rolling forms instead of base components
- Does the component implement a custom form for single-record CRUD where
lightning-record-form, lightning-record-edit-form, or lightning-record-view-form would suffice?
- Does validation logic duplicate what base record form components provide natively?
- Are standard SLDS styles recreated manually instead of leveraging the styling baked into base components?
2. Not importing references
- Are object or field API names referenced as hard-coded strings?
- In templates, are field values accessed directly via expressions like
record.data.fields.Name.value without schema imports?
- Does the JS file lack any
@salesforce/schema import even though it interacts with Salesforce fields?
3. Mixing Apex and LDS without synchronization
- Does the component read via LDS and mutate the same record through Apex without a subsequent cache refresh?
- Does it fetch through Apex yet rely on the LDS cache for display without synchronizing after updates?
- Do multiple data sources touch the same object without an explicit refresh strategy?
4. Overusing Apex
- Does the component call Apex solely to retrieve or update a single record that
getRecord, updateRecord, or a base record form could handle?
- Is Apex used to run a simple SOQL query whose fields are available through standard LDS wire adaptors?
- Are custom Apex methods present for basic CRUD while no LDS/UIAPI calls appear in the code?
Workflow
Step 1 — Inventory the data layer
List every data operation in the component:
- Wire adapters (
@wire(getRecord, …), @wire(getRecords, …), @wire(someApexMethod, …)).
- Imperative calls (
updateRecord, createRecord, deleteRecord, Apex imperative).
- Reads vs writes, target object(s), fields, and whether the refresh path after writes is wired.
Step 2 — Run the four-section checklist
Walk sections §1–§4 in order. For each section, decide whether it applies to the component under review and record the result in the report. Use the shape below — every section must appear exactly once, either as an issue (violation) under ## LDS Best Practices, or as a compliant entry under ## Sections checked (no issue). Finish with a ## Summary line listing counts and a one-paragraph narrative.
Report shape:
## LDS Best Practices
- §<N> <section title> — <file>:<lines>
Issue: <what is wrong, specifically citing the pattern in the code>
Fix: <the corrective change, naming the exact import / API to use>
Applied: <yes | no>
## Sections checked (no issue)
- §<N> <section title> — <file>:<lines>
Status: Compliant (no action).
Evidence: <what in the code makes this section compliant — cite lines, imports, and the base component or schema token being used>
## Summary
- <X> issue(s) found; <Y> fixed; <Z> deferred.
- <one-paragraph narrative of the review — what the component does, why the flagged issues matter, and why the compliant sections are compliant.>
Rules for producing this report:
- Every one of §1–§4 must appear in exactly one of the two blocks. Do not omit a section because it is compliant; record it with evidence under
## Sections checked (no issue).
- Under
## LDS Best Practices, only list actual violations. If there are no violations, write "No best-practice issues found." as the first line, then move every section to the compliant block.
- Cite specific file paths and line ranges from the component under review — never generic references.
- Do not invent additional sections beyond §1–§4; downstream a11y / RTL / security reviews run as separate workflows and have their own reports.
Step 3 — Apply referential-integrity fixes
For every hard-coded API name:
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';
Use these constants everywhere the object or field is referenced — wire configs, @wire field arrays, getFieldValue(record, NAME_FIELD) calls, and base-component object-api-name / fields attributes.
Full rules: references/lds-referential-integrity.md.
Step 4 — Apply data-consistency fixes
- After imperative LDS mutation (
updateRecord, createRecord, deleteRecord), dispatch a refresh:import { updateRecord, getRecord } from 'lightning/uiRecordApi';
import { refreshApex } from '@salesforce/apex';
async handleSave() {
await updateRecord({ fields: { Id: this.recordId, Name: this.name } });
await refreshApex(this.wiredRecord);
}
- After Apex mutation of a record the cache holds, prefer:
import { notifyRecordUpdateAvailable } from 'lightning/uiRecordApi';
await notifyRecordUpdateAvailable([{ recordId: this.recordId }]);
- Keep a reference to wire results (
this.wiredRecord = result; return result.data;) so refreshApex can target them.
- Base form components refresh themselves; no manual refresh needed.
Full rules: references/lds-data-consistency.md.
Step 5 — Replace Apex with UIAPI where applicable
- Single-record read →
getRecord (with fields + schema imports).
- Single-record update →
updateRecord or lightning-record-edit-form.
- Single-record create →
createRecord or lightning-record-form with mode="edit".
- Related-record read →
getRelatedListRecords.
- Picklist values →
getPicklistValues.
- Object metadata →
getObjectInfo / getObjectInfos.
When in doubt about adapter shape, grep references/adapter-apis.md for the backticked adapter name (e.g. # `getRecord`) — it has the authoritative parameters, returns, and usage. For @salesforce/schema/<Object>.<Field> paths, confirm the exact API name in Setup → Object Manager → <Object> → Details → API Name. For unfamiliar return types, grep references/wire-adapter-types.md for the type name.
Step 6 — Verify
- No hardcoded API names remain in the component files.
- Every write path has a matching refresh path (or uses a base form component).
- No duplicate reads of the same record via both Apex and UIAPI.
- Existing Jest tests pass; add coverage for the refresh flow (
refreshApex called exactly once per mutation).
Cross-References
- Related skills:
experience-lwc-generate — when the review surfaces the need to regenerate rather than patch the component.
design-systems-slds-apply — for SLDS class / design-token cleanup surfaced by the LDS review.
- Adjacent (out-of-band today):
- GraphQL query/mutation authoring, upstream data-requirements analysis, and the security / RTL / a11y review passes run as separate workflows with their own tooling.
Examples
Base-component first (preferred)
<template>
<lightning-record-form
record-id={recordId}
object-api-name="Account"
fields={fields}
mode="edit"
</lightning-record-form>
</template>
import { LightningElement, api } from 'lwc';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';
export default class AccountEditor extends LightningElement {
@api recordId;
fields = [NAME_FIELD, INDUSTRY_FIELD];
handleSuccess() {
this.dispatchEvent(new CustomEvent('saved'));
}
}
Imperative update with refresh
import { LightningElement, api, wire } from 'lwc';
import { getRecord, updateRecord } from 'lightning/uiRecordApi';
import { refreshApex } from '@salesforce/apex';
import ACCOUNT_NAME from '@salesforce/schema/Account.Name';
export default class RenameAccount extends LightningElement {
@api recordId;
wiredRecord;
@wire(getRecord, { recordId: '$recordId', fields: [ACCOUNT_NAME] })
wired(result) {
this.wiredRecord = result;
}
async handleRename(event) {
await updateRecord({ fields: { Id: this.recordId, Name: event.detail } });
await refreshApex(this.wiredRecord);
}
}
Verification
- Grep for
@salesforce/schema/ imports — they should cover every field/object the component references.
- Grep for string literals that look like API names (
'Account', 'Name') — none should appear in wire configs or field arrays.
- Trace every mutation call to a refresh call (either
refreshApex, notifyRecordUpdateAvailable, or a base form handling it internally).
- Confirm Apex is only used where UIAPI can't satisfy the requirement (bulk, complex joins, custom logic).
1---2name: experience-lds-best-practices-apply3description: Use when reviewing or implementing Lightning Data Service best practices in an LWC (.js, .html, .js-meta.xml) — UIAPI vs Apex, refreshApex / notifyRecordUpdateAvailable, @salesforce/schema imports, LDS record-form data patterns. TRIGGER on "apply LDS best practices to this LWC", "review this LWC for LDS best-practice issues", "review this component for Lightning Data Service issues", "UIAPI or Apex for this data?", "fix stale data after record save", "sync LDS cache", "use @salesforce/schema for field names", "choose between getRecord and Apex". DO NOT TRIGGER when building a new LWC (use experience-lwc-generate), applying SLDS design tokens (use design-systems-slds-apply), picking or wiring a `lightning-*` base component's props/events/slots generically (use experience-lwc-base-components-integrate — this skill covers only the LDS data-layer rationale, even when the fix involves a base record form), or for security / RTL / accessibility reviews (separate passes).4---5<!-- adk-managed-skill -->
6
7# Applying LDS Best Practices
8
9Apply the Lightning Data Service guidelines to a Lightning Web Component. Three pillars: **data consistency**, **referential integrity**, and **UIAPI vs Apex**. Focused on the UI API path — GraphQL and upstream data-requirements analysis are handled out-of-band.
10
11## When to Use
12
13- Reviewing a component's data layer for LDS compliance (hand-rolled forms, stringly-typed field names, un-synchronized Apex + LDS, Apex overuse).
14- Implementing CRUD on standard or custom objects.
15- Deciding between `getRecord`, `getRecords`, `createRecord`, `updateRecord`, `deleteRecord`, base record form components, or Apex.
16- Fixing stale-data bugs after record mutation.
17- Adding schema imports (`@salesforce/schema/...`) to replace hard-coded field/object names.
18
19Do NOT use this skill for:
20- GraphQL query/mutation generation — handled out-of-band today.
21- Upstream data-requirements discovery — handled out-of-band today.
22- SLDS class / design-token work (use `design-systems-slds-apply`).
23- Accessibility, security, or RTL review — those are separate passes run with their own tooling.
24
25## Prerequisites
26
27- Component path.
28- Understanding of the component's data operations (read / write / both) and whether Apex is already involved.
29- Access to the org's schema for `@salesforce/schema` imports (Setup → Object Manager → `<Object>` → Details → API Name; or, when GraphQL serves the read, an SDL pulled from the target org).
30
31## Knowledge Bases
32
33- [references/lds-expert.md](references/lds-expert.md) — authoritative LDS knowledge (patterns, adapters, caching, mutation flows).
34- [references/lds-data-consistency.md](references/lds-data-consistency.md) — cache invalidation, `refreshApex`, `notifyRecordUpdateAvailable`, wire result propagation.
35- [references/lds-referential-integrity.md](references/lds-referential-integrity.md) — `@salesforce/schema` imports, field constants, object-name resolution, and their propagation through refactors.
36
37**Per-adapter API reference** — [references/adapter-apis.md](references/adapter-apis.md) holds Syntax / Parameters / Returns / Usage for every UI API adapter, grouped by family (`uiRecordApis`, `uiListsApis`, `uiRelatedListApis`, `uiObjectInfoApis`). Each adapter is a `` # `<name>` `` block; grep for the backticked name (e.g. `` # `getRecord` ``) to jump to its entry. Read this before wiring an adapter; do not paraphrase from memory.
38
39**Type catalog** — [references/wire-adapter-types.md](references/wire-adapter-types.md) holds every type the adapters return (`Record`, `ObjectInfo`, `FieldValue`, etc.), grouped by category and rendered with the same formatter the legacy MCP tool used. Grep for `## <TypeName>` to jump to a specific entry.
40
41Read the applicable reference before editing code.
42
43## Core Principles
44
451. Prefer **LDS/UIAPI** for CRUD on standard and custom objects. Use Apex **only** when business logic or bulk operations exceed LDS capabilities.
462. Always keep rendered data fresh with `refreshApex(wiredResult)` **or** `notifyRecordUpdateAvailable([{ recordId }])` after any mutation.
473. Import object and field references from `@salesforce/schema` — not string literals. This protects the component against metadata renames.
484. Favor base record form components (`lightning-record-form`, `lightning-record-edit-form`, `lightning-record-view-form`) for single-record UIs. They ship with validation, SLDS styling, accessibility, and field-level security.
49
50## Review Checklist
51
52Answer **Yes / No** to each. Any **Yes** triggers a refactor.
53
54### 1. Hand-rolling forms instead of base components
55- Does the component implement a custom form for single-record CRUD where `lightning-record-form`, `lightning-record-edit-form`, or `lightning-record-view-form` would suffice?
56- Does validation logic duplicate what base record form components provide natively?
57- Are standard SLDS styles recreated manually instead of leveraging the styling baked into base components?
58
59### 2. Not importing references
60- Are object or field API names referenced as hard-coded strings?
61- In templates, are field values accessed directly via expressions like `record.data.fields.Name.value` without schema imports?
62- Does the JS file lack any `@salesforce/schema` import even though it interacts with Salesforce fields?
63
64### 3. Mixing Apex and LDS without synchronization
65- Does the component read via LDS and mutate the same record through Apex without a subsequent cache refresh?
66- Does it fetch through Apex yet rely on the LDS cache for display without synchronizing after updates?
67- Do multiple data sources touch the same object without an explicit refresh strategy?
68
69### 4. Overusing Apex
70- Does the component call Apex solely to retrieve or update a single record that `getRecord`, `updateRecord`, or a base record form could handle?
71- Is Apex used to run a simple SOQL query whose fields are available through standard LDS wire adaptors?
72- Are custom Apex methods present for basic CRUD while no LDS/UIAPI calls appear in the code?
73
74## Workflow
75
76### Step 1 — Inventory the data layer
77
78List every data operation in the component:
79
80- Wire adapters (`@wire(getRecord, …)`, `@wire(getRecords, …)`, `@wire(someApexMethod, …)`).
81- Imperative calls (`updateRecord`, `createRecord`, `deleteRecord`, Apex imperative).
82- Reads vs writes, target object(s), fields, and whether the refresh path after writes is wired.
83
84### Step 2 — Run the four-section checklist
85
86Walk sections §1–§4 in order. For each section, decide whether it applies to the component under review and record the result in the report. Use the shape below — every section must appear exactly once, either as an **issue** (violation) under `## LDS Best Practices`, or as a **compliant** entry under `## Sections checked (no issue)`. Finish with a `## Summary` line listing counts and a one-paragraph narrative.
87
88**Report shape:**
89
90```markdown
91## LDS Best Practices
92
93- §<N> <section title> — <file>:<lines>
94 Issue: <what is wrong, specifically citing the pattern in the code>
95 Fix: <the corrective change, naming the exact import / API to use>
96 Applied: <yes | no>
97
98## Sections checked (no issue)
99
100- §<N> <section title> — <file>:<lines>
101 Status: Compliant (no action).
102 Evidence: <what in the code makes this section compliant — cite lines, imports, and the base component or schema token being used>
103
104## Summary
105
106- <X> issue(s) found; <Y> fixed; <Z> deferred.
107- <one-paragraph narrative of the review — what the component does, why the flagged issues matter, and why the compliant sections are compliant.>
108```
109
110Rules for producing this report:
111
112- Every one of §1–§4 must appear in exactly one of the two blocks. Do not omit a section because it is compliant; record it with evidence under `## Sections checked (no issue)`.
113- Under `## LDS Best Practices`, only list actual violations. If there are no violations, write "No best-practice issues found." as the first line, then move every section to the compliant block.
114- Cite specific file paths and line ranges from the component under review — never generic references.
115- Do not invent additional sections beyond §1–§4; downstream a11y / RTL / security reviews run as separate workflows and have their own reports.
116
117### Step 3 — Apply referential-integrity fixes
118
119For every hard-coded API name:
120
121```javascript
122import ACCOUNT_OBJECT from '@salesforce/schema/Account';
123import NAME_FIELD from '@salesforce/schema/Account.Name';
124import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';
125```
126
127Use these constants everywhere the object or field is referenced — wire configs, `@wire` field arrays, `getFieldValue(record, NAME_FIELD)` calls, and base-component `object-api-name` / `fields` attributes.
128
129Full rules: [references/lds-referential-integrity.md](references/lds-referential-integrity.md).
130
131### Step 4 — Apply data-consistency fixes
132
133- After **imperative** LDS mutation (`updateRecord`, `createRecord`, `deleteRecord`), dispatch a refresh:
134 ```javascript
135 import { updateRecord, getRecord } from 'lightning/uiRecordApi';
136 import { refreshApex } from '@salesforce/apex';
137
138 async handleSave() {
139 await updateRecord({ fields: { Id: this.recordId, Name: this.name } });
140 await refreshApex(this.wiredRecord);
141 }
142 ```
143- After **Apex** mutation of a record the cache holds, prefer:
144 ```javascript
145 import { notifyRecordUpdateAvailable } from 'lightning/uiRecordApi';
146 await notifyRecordUpdateAvailable([{ recordId: this.recordId }]);
147 ```
148- Keep a reference to wire results (`this.wiredRecord = result; return result.data;`) so `refreshApex` can target them.
149- Base form components refresh themselves; no manual refresh needed.
150
151Full rules: [references/lds-data-consistency.md](references/lds-data-consistency.md).
152
153### Step 5 — Replace Apex with UIAPI where applicable
154
155- Single-record read → `getRecord` (with `fields` + schema imports).
156- Single-record update → `updateRecord` or `lightning-record-edit-form`.
157- Single-record create → `createRecord` or `lightning-record-form` with `mode="edit"`.
158- Related-record read → `getRelatedListRecords`.
159- Picklist values → `getPicklistValues`.
160- Object metadata → `getObjectInfo` / `getObjectInfos`.
161
162When in doubt about adapter shape, grep [references/adapter-apis.md](references/adapter-apis.md) for the backticked adapter name (e.g. `` # `getRecord` ``) — it has the authoritative parameters, returns, and usage. For `@salesforce/schema/<Object>.<Field>` paths, confirm the exact API name in Setup → Object Manager → `<Object>` → Details → API Name. For unfamiliar return types, grep [references/wire-adapter-types.md](references/wire-adapter-types.md) for the type name.
163
164### Step 6 — Verify
165
166- No hardcoded API names remain in the component files.
167- Every write path has a matching refresh path (or uses a base form component).
168- No duplicate reads of the same record via both Apex and UIAPI.
169- Existing Jest tests pass; add coverage for the refresh flow (`refreshApex` called exactly once per mutation).
170
171## Cross-References
172
173- Related skills:
174 - `experience-lwc-generate` — when the review surfaces the need to regenerate rather than patch the component.
175 - `design-systems-slds-apply` — for SLDS class / design-token cleanup surfaced by the LDS review.
176- Adjacent (out-of-band today):
177 - GraphQL query/mutation authoring, upstream data-requirements analysis, and the security / RTL / a11y review passes run as separate workflows with their own tooling.
178
179## Examples
180
181**Base-component first (preferred)**
182
183```html
184<template>
185 <lightning-record-form
186 record-id={recordId}
187 object-api-name="Account"
188 fields={fields}
189 mode="edit"
190 onsuccess={handleSuccess}>
191 </lightning-record-form>
192</template>
193```
194
195```javascript
196import { LightningElement, api } from 'lwc';
197import NAME_FIELD from '@salesforce/schema/Account.Name';
198import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';
199
200export default class AccountEditor extends LightningElement {
201 @api recordId;
202 fields = [NAME_FIELD, INDUSTRY_FIELD];
203
204 handleSuccess() {
205 this.dispatchEvent(new CustomEvent('saved'));
206 }
207}
208```
209
210**Imperative update with refresh**
211
212```javascript
213import { LightningElement, api, wire } from 'lwc';
214import { getRecord, updateRecord } from 'lightning/uiRecordApi';
215import { refreshApex } from '@salesforce/apex';
216import ACCOUNT_NAME from '@salesforce/schema/Account.Name';
217
218export default class RenameAccount extends LightningElement {
219 @api recordId;
220 wiredRecord;
221
222 @wire(getRecord, { recordId: '$recordId', fields: [ACCOUNT_NAME] })
223 wired(result) {
224 this.wiredRecord = result;
225 }
226
227 async handleRename(event) {
228 await updateRecord({ fields: { Id: this.recordId, Name: event.detail } });
229 await refreshApex(this.wiredRecord);
230 }
231}
232```
233
234## Verification
235
236- Grep for `@salesforce/schema/` imports — they should cover every field/object the component references.
237- Grep for string literals that look like API names (`'Account'`, `'Name'`) — none should appear in wire configs or field arrays.
238- Trace every mutation call to a refresh call (either `refreshApex`, `notifyRecordUpdateAvailable`, or a base form handling it internally).
239- Confirm Apex is only used where UIAPI can't satisfy the requirement (bulk, complex joins, custom logic).