# What is Lightning Data Service (LDS) in LWC, and what are the benefits of using lightning/uiRecordApi?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Lightning Data Service, uiRecordApi

Read it on the site: https://www.forcechallenges.com/interview-questions/what-is-lightning-data-service-lds-in-lwc-and-what-are-the-benefits-of-using-lightning-uirecordapi

## Answer

**Basics:** Instead of writing an Apex controller for every read or save, you ask LDS for a record by Id and fields. It handles caching, so two components showing the same record make one request, and an edit in one updates the other automatically.

**How it works:**
1. Import adapters and functions from `lightning/uiRecordApi`: `getRecord` (wire), `getFieldValue`, `getFieldDisplayValue`, `createRecord`, `updateRecord`, `deleteRecord`, `getRecordNotifyChange`/`notifyRecordUpdateAvailable`, and helpers like `generateRecordInputForUpdate`.
2. Import field references from schema: `import NAME from '@salesforce/schema/Account.Name';` which gives compile-time checking and dependency tracking.
3. Wire: `@wire(getRecord, { recordId: '$recordId', fields: [NAME] })`.
4. Write: `updateRecord({ fields: { Id, [NAME.fieldApiName]: value } })`.
5. LDS invalidates and refreshes affected wires after writes.

**Advanced / deep dive:** Benefits: less code, no Apex test coverage burden, consistent security (CRUD, FLS and sharing enforced), cache shared across components, and performance. Limits: it works one record at a time, no aggregate queries, no complex joins or multi-object transactions, limited to what the UI API supports, and every field must be listed (use `optionalFields` when a user may lack access, to avoid failing the whole request). `getRecordUi` is a legacy approach that returns layout metadata; prefer `getRecord` and `getObjectInfo` (verify current guidance in release notes). Reach for Apex for bulk operations, business logic across objects, or SOQL beyond UI API capability. LDS also underpins base components such as `lightning-record-form` and works in Salesforce mobile with offline support for supported scenarios. Components with `@api recordId` on record pages get the Id automatically.

**Code / config example:**
```javascript
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue, updateRecord } from 'lightning/uiRecordApi';
import NAME from '@salesforce/schema/Account.Name';
import ID_FIELD from '@salesforce/schema/Account.Id';

export default class AccountEditor extends LightningElement {
    @api recordId;
    @wire(getRecord, { recordId: '$recordId', fields: [NAME] }) account;

    get name() { return getFieldValue(this.account.data, NAME); }

    async rename(newName) {
        const fields = { [ID_FIELD.fieldApiName]: this.recordId, [NAME.fieldApiName]: newName };
        await updateRecord({ fields });   // wires refresh automatically
    }
}
```

**Hidden gotchas & must-remember points:**
- `updateRecord` fields must include `Id`.
- `getRecord` fails entirely if any required field is inaccessible; use `optionalFields` for uncertain fields.
- LDS handles one record per call, so it is not for bulk updates.
- It respects sharing and FLS automatically, unlike Apex without `with sharing`/`USER_MODE`.
- Use schema imports instead of hard-coded strings for field names.
- Writes by Apex are not seen until you notify LDS or refresh.

**Best practice:** Default to LDS for single-record CRUD, and use Apex only for logic, bulk work and queries LDS cannot express.

---
Source: ForceChallenges · last updated 2026-11-02
