Sub topic 8
Extracting values with getFieldValue and getFieldDisplayValue
This chapter assumes you can write a basic Lightning web component and use @wire(getRecord) with a recordId. You should also know how to import field references from @salesforce/schema. The chapter covers how to read values out of the object that getRecord returns, and when to use getFieldValue and when to use getFieldDisplayValue.
Why you need helper functions
When getRecord succeeds, data holds a record object. A field value is not stored directly on the record. Each field sits under fields, and each entry has two properties, value and displayValue. A simplified record looks like this:
const record = {
id: '001xx000003DGb2AAG',
apiName: 'Account',
fields: {
Name: { value: 'Acme', displayValue: null },
Industry: { value: 'Biotechnology', displayValue: 'Biotechnology' },
AnnualRevenue: { value: 1500000, displayValue: '$1,500,000' }
}
};You could write record.fields.Name.value yourself. That code is fragile for three reasons:
dataisundefineduntil the wire provisions a result, so the chain throws aTypeError.- A field you asked for through
optionalFieldsmay be missing fromfieldsif the user cannot see it. - Hard-coded strings like
'Name'bypass the schema imports, so a renamed or deleted field is not caught at deploy time.
The helpers getFieldValue and getFieldDisplayValue handle all three problems. Both are exported from lightning/uiRecordApi.
getFieldValue
getFieldValue(record, field) returns the raw value of a field. It takes two arguments:
record: the record object, normallythis.account.data.field: a field reference imported from@salesforce/schema, such asNAME_FIELD.
It returns undefined when the record is undefined or null, or when the field is not in the record. This is why you can call it safely before the wire has returned.
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import REVENUE_FIELD from '@salesforce/schema/Account.AnnualRevenue';
export default class AccountRevenue extends LightningElement {
@api recordId;
@wire(getRecord, {
recordId: '$recordId',
fields: [NAME_FIELD, REVENUE_FIELD]
})
account;
get name() {
return getFieldValue(this.account.data, NAME_FIELD);
}
get revenue() {
return getFieldValue(this.account.data, REVENUE_FIELD);
}
}Here revenue is the number 1500000, not a formatted string. Use the raw value when you do arithmetic, compare values, send the value to Apex, or pass it to a component that formats it for you, such as lightning-formatted-number.
getFieldDisplayValue
getFieldDisplayValue(record, field) returns the displayValue of a field. Salesforce computes this string on the server, using the running user's locale, currency, time zone, and picklist labels. For example, a currency field with the value 1500000 can have the display value $1,500,000.
Use it when you want to show what the user would see in standard Salesforce UI:
- Picklist fields, where the display value is the translated label and the value is the API value.
- Currency, date, and date-time fields, which are formatted for the user's locale.
- Lookup fields, where the value is a record ID. The display value is the related record's name when that name is available.
import { getFieldDisplayValue } from 'lightning/uiRecordApi';
get displayRevenue() {
return getFieldDisplayValue(this.account.data, REVENUE_FIELD);
}When displayValue is missing
The server does not always send a display value. For a plain text field such as Name, displayValue is null, and getFieldDisplayValue returns null. If you rely on it for every field, some of them will render blank.
A common defensive pattern falls back to the raw value:
get industry() {
return (
getFieldDisplayValue(this.account.data, INDUSTRY_FIELD) ??
getFieldValue(this.account.data, INDUSTRY_FIELD)
);
}The ?? operator only falls back for null and undefined, so an empty string or 0 is kept as is.
Choosing between the two
| Need | Use | Reason |
|---|---|---|
| Show a picklist label | `getFieldDisplayValue` | The value is the API value, which can differ from the label. |
| Show a formatted currency or date | `getFieldDisplayValue` | The server formats it for the user's locale. |
| Calculate or compare numbers | `getFieldValue` | The display value is a string. |
| Send a value back to Apex or `updateRecord` | `getFieldValue` | The server expects the raw value. |
| Show a plain text field | `getFieldValue` | Text fields usually have no display value. |
| Read a lookup's record ID | `getFieldValue` | The value is the ID. |
Using the values in a template
Getters keep the template simple. The template checks account.data before it reads anything, which avoids showing empty labels while the wire is loading.
<template>
<template lwc:if={account.data}>
<p>Name: {name}</p>
<p>Revenue: {displayRevenue}</p>
</template>
<template lwc:if={account.error}>
<p>The record could not be loaded.</p>
</template>
</template>The getters re-run when account changes, because the wire reassigns the property each time it provisions a new result. You do not need to track anything yourself.
Reading related and spanning fields
Field references can reach through a relationship. Importing @salesforce/schema/Account.Owner.Name gives you a reference to the owner's name. You request it in fields or optionalFields, and then read it with the same helper:
import OWNER_NAME_FIELD from '@salesforce/schema/Account.Owner.Name';
@wire(getRecord, {
recordId: '$recordId',
fields: [NAME_FIELD],
optionalFields: [OWNER_NAME_FIELD]
})
account;
get ownerName() {
return getFieldValue(this.account.data, OWNER_NAME_FIELD);
}The helper follows the relationship for you. Without it you would walk fields.Owner.value.fields.Name.value by hand, and each step could be null if the lookup is empty.
Common errors
- Reading a field that was never requested.
getFieldValuereturnsundefinedfor any field you left out offieldsandoptionalFields. The helper cannot fetch data. Add the field to the wire configuration. - Passing the wrong first argument. Pass
this.account.data, notthis.account. The wire result is an object withdataanderror, and the helper expects the record. - Using a string instead of a schema import.
getFieldValue(this.account.data, 'Name')does not work. Pass the imported field reference. - Showing a display value where a value is needed. A formatted string such as
'$1,500,000'fails numeric checks and breaksupdateRecordif you send it back. - Assuming a value is never
null. A field with no data returnsnullfromgetFieldValue. Handle that case in the template or in the getter.
How the helpers fit the data flow
The diagram shows where the helpers sit between the wire and the template.
Using the helpers inside a function wire
The helpers work the same way when you wire to a function. The function receives { data, error }, so you pass data directly:
@wire(getRecord, { recordId: '$recordId', fields: [NAME_FIELD] })
wiredAccount({ data, error }) {
if (data) {
this.name = getFieldValue(data, NAME_FIELD);
this.errorMessage = undefined;
} else if (error) {
this.errorMessage = error.body && error.body.message;
this.name = undefined;
}
}This style suits cases where you need to run logic once per result, such as storing a copy of the value to edit. For plain display, a getter is shorter and always current.
Summary
getFieldValuereturns the raw value and is the right choice for logic, calculations, and saving.getFieldDisplayValuereturns the formatted label or string for the user and can benullfor fields such as plain text.- Both accept the record and a schema field reference, and both return
undefinedornullinstead of throwing when data is missing. - Request every field you read, through
fieldsoroptionalFields.
Exercise
A picklist field Account.Rating has the API value Hot and a translated label Chaud for a French user. Which helper shows the label, and which helper gives the value you should send to updateRecord?
Show answer
getFieldDisplayValue(record, RATING_FIELD) returns the label Chaud for display. getFieldValue(record, RATING_FIELD) returns the API value Hot, which is the value to send to updateRecord.
Exercise
A getter calls getFieldDisplayValue(this.account.data, NAME_FIELD) and the template shows nothing, although getFieldValue returns the name. Why, and how do you fix it?
Show answer
Name is a plain text field, and the server sends no display value for it, so the helper returns null. Use getFieldValue for the field, or fall back to it with getFieldDisplayValue(...) ?? getFieldValue(...).
Exercise
A component calls getFieldValue(this.account, NAME_FIELD) and always gets undefined, even after the record loads. What is the mistake?
Show answer
The first argument is the wire result object, which holds data and error. It must be the record, so the call should be getFieldValue(this.account.data, NAME_FIELD).