Sub topic 8
Extracting values with getFieldValue and getFieldDisplayValue
This chapter assumes you can write a Lightning web component, use @api recordId, and wire getRecord to a property. You will learn how to read field values out of the wire result without digging through the raw response yourself.
Why not read the response directly
The data object returned by getRecord has a nested shape. Each field sits under data.fields, and each field holds a value and a displayValue. Reading data.fields.Name.value works, but it breaks when data is still undefined, and it repeats API names as strings. The helper functions avoid both problems.
Both helpers come from lightning/uiRecordApi. They take the record data and a field reference imported from @salesforce/schema.
getFieldValue
getFieldValue(record, field) returns the raw value of a field. That is the value as stored: a number for a currency field, an ISO string for a date, and an ID for a lookup.
If record is undefined or the field was not returned, the function returns undefined. This means you can call it in a getter before the wire has provisioned data.
getFieldDisplayValue
getFieldDisplayValue(record, field) returns the value formatted for the user, based on their locale and the field type. A picklist returns its label instead of its API value. A date returns a formatted date string.
For some field types, such as plain text, Lightning Data Service returns no separate display value. In that case getFieldDisplayValue returns undefined. Use getFieldValue for those fields.
| Field type | getFieldValue | getFieldDisplayValue |
|---|---|---|
| Text (Name) | The text | Usually `undefined` |
| Picklist (Industry) | API value | Label shown to the user |
| Currency | A number | Formatted with currency symbol |
| Lookup | Record ID | Name of the related record, when available |
Worked example
The component below requires Name and requests Industry as an optional field. It reads both through getters.
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue, getFieldDisplayValue } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';
export default class AccountSummary extends LightningElement {
@api recordId;
@wire(getRecord, {
recordId: '$recordId',
fields: [NAME_FIELD],
optionalFields: [INDUSTRY_FIELD]
})
account;
get name() {
return getFieldValue(this.account.data, NAME_FIELD);
}
get industry() {
return getFieldDisplayValue(this.account.data, INDUSTRY_FIELD);
}
}The getters pass this.account.data, not this.account. The wired property is an object with data and error keys, and the helpers need the record inside data.
The template renders the values only when data exists:
<template>
<template lwc:if={account.data}>
<p>Name: {name}</p>
<p>Industry: {industry}</p>
</template>
<template lwc:if={account.error}>
<p>The record could not be loaded.</p>
</template>
</template>Common errors
- Passing
this.accountinstead ofthis.account.data. The helper finds no fields and returnsundefined, with no error. - Forgetting to request the field. A field that is in neither
fieldsnoroptionalFieldsis not in the response, so the helper returnsundefined. - Expecting a display value for every field. Use
getFieldValuewhengetFieldDisplayValuereturnsundefined. - Using a string such as
'Account.Name'. The helpers expect the imported schema reference.
Exercise
A picklist field Industry holds the API value "Banking" with the label "Banking and Finance". Which helper returns the label, and what does the other return?
Show answer
getFieldDisplayValue returns the label "Banking and Finance". getFieldValue returns the stored API value "Banking". Both return undefined if the record data is not yet available or the field was not requested.
Exercise
A getter calls getFieldValue(this.account, NAME_FIELD) and always returns undefined, although the record loads. What is wrong?
Show answer
It passes the whole wire result instead of its data property. The call should be getFieldValue(this.account.data, NAME_FIELD).