# How do getFieldValue and getFieldDisplayValue simplify extracting record field data in LWC?

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

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-getfieldvalue-and-getfielddisplayvalue-simplify-extracting-record-field-data-in-lwc

## Answer

**Basics:** `getRecord` returns a nested structure (`record.fields.Name.value`, `record.fields.Name.displayValue`). Reading it manually is verbose and breaks on relationship fields. The helper functions from `lightning/uiRecordApi` take the record and a field reference imported from `@salesforce/schema` and return the value safely, including through relationships such as `Account.Owner.Name`.

**How it works:** Import the field: `import NAME from '@salesforce/schema/Account.Name';`. Wire `getRecord` with `recordId: '$recordId'` and `fields: [NAME]` (or `optionalFields`). In a getter use `getFieldValue(this.account.data, NAME)`. For picklists, dates, currencies and other types where the user-visible form differs, `getFieldDisplayValue` returns the label or locale-formatted string. If the record or field is missing, `getFieldValue` returns `undefined`.

**Advanced / deep dive:** The schema imports give compile-time validation and keep field references refactor-safe and dependency-aware (deleting a field is blocked while referenced). `fields` fails the whole request if the user lacks access to any field, while `optionalFields` silently skips inaccessible ones, which is why optional is safer for shared components. `getFieldDisplayValue` returns `undefined` when the API provides no display value (many text fields have none), so fall back to `getFieldValue`. Data comes from the LDS client cache, so multiple components share one request and see updates after `updateRecord` or `notifyRecordUpdateAvailable`. Cross-object paths cost nothing extra on the client but the field must be reachable through a relationship the user can see. Use `getRecord` for single records; use `uiListApi` or Apex for sets of records.

**Code / config example:**
```javascript
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue, getFieldDisplayValue } from 'lightning/uiRecordApi';
import STAGE from '@salesforce/schema/Opportunity.StageName';
import AMOUNT from '@salesforce/schema/Opportunity.Amount';
import OWNER from '@salesforce/schema/Opportunity.Owner.Name';

export default class OppSummary extends LightningElement {
    @api recordId;
    @wire(getRecord, { recordId: '$recordId', fields: [STAGE, AMOUNT], optionalFields: [OWNER] })
    opp;

    get stage()  { return getFieldValue(this.opp.data, STAGE); }
    get amount() { return getFieldDisplayValue(this.opp.data, AMOUNT); } // e.g. "$1,200.00"
    get owner()  { return getFieldValue(this.opp.data, OWNER); }
}
```

**Hidden gotchas & must-remember points:**
- Fields not requested in `getRecord` are not in the data, so getters return `undefined`.
- A single inaccessible field in `fields` makes the whole wire error; use `optionalFields` for flexible components.
- `getFieldDisplayValue` can be `undefined`; fall back to the raw value.
- Do not hard-code field API names as strings; use schema imports.
- Wire results are read-only objects.
- Changes made via Apex DML will not appear until you refresh the LDS cache.

**Best practice:** Wrap field access in getters using schema imports and use display values for UI text while keeping raw values for logic.

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