Sub topic 6
Choosing fields or optionalFields
This chapter assumes you can write a Lightning web component with an HTML template, a JavaScript class, and a metadata file. It also assumes you know the @api recordId pattern on record pages, and that you have already wired getRecord to a property. Here you decide which fields go in fields and which go in optionalFields, and you learn what each choice does when a field is missing or hidden.
The two parameters
The getRecord wire adapter accepts two parameters that list the fields to load. Both take an array of field references imported from @salesforce/schema.
fieldslists fields the component cannot work without.optionalFieldslists fields the component can work without.
You must pass at least one of them, or the adapter has nothing to request. A wire that sets only optionalFields is valid.
import { LightningElement, api, wire } from 'lwc';
import { getRecord } 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;
}Here Name is required and Industry is optional.
What changes at runtime
The difference appears when the running user cannot read a field, or when the field does not exist in the org. A field can be unreadable because of field-level security. A field can also be absent because it belongs to a managed package that is not installed, or because a field was deleted.
| Situation | Field in `fields` | Field in `optionalFields` |
|---|---|---|
| User can read the field | Value is returned | Value is returned |
| User lacks field-level security access | The whole request fails and `error` is set | The request succeeds and the field is left out of the result |
| Field does not exist in the org | The whole request fails and `error` is set | The request succeeds and the field is left out of the result |
| The record itself is not accessible | `error` is set | `error` is set |
With fields, one bad field costs you the entire record. With optionalFields, the record still loads and the unavailable field is simply missing from data.fields.
Why this matters for reusable components
A component placed on a record page runs for many profiles and permission sets. If it lists a sensitive field in fields, any user without access to that field sees an error instead of the record. The component looks broken for them even though most of its content could have been shown.
A second case is a component shipped in a managed package or shared across orgs. A custom field might exist in one org and not in another. Putting it in optionalFields lets the same component run in both.
Use these rules of thumb:
- Put a field in
fieldswhen the component is meaningless without it, and a clear error is better than a half-empty view. A record title is a typical example. - Put a field in
optionalFieldswhen the component can hide or replace the value. Secondary details, such as industry or phone, usually qualify. - When access varies by user or org and you are unsure, choose
optionalFields.
Reading optional values safely
A field in optionalFields may be absent, so code that reads it must tolerate that. The getFieldValue and getFieldDisplayValue helpers return undefined when the field is not in the result. Direct property access such as data.fields.Industry.value throws a TypeError when Industry is missing, so avoid it for optional fields.
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';
export default class AccountIndustry 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 getFieldValue(this.account.data, INDUSTRY_FIELD);
}
get hasIndustry() {
return this.industry !== undefined && this.industry !== null;
}
}The template then shows the optional line only when a value exists.
<template>
<template lwc:if={account.data}>
<p>Name: {name}</p>
<template lwc:if={hasIndustry}>
<p>Industry: {industry}</p>
</template>
</template>
<template lwc:if={account.error}>
<p>The record could not be loaded.</p>
</template>
</template>Note the difference between two empty cases. A field the user can read but that has no value returns null. A field that was requested as optional but was omitted returns undefined. The hasIndustry getter treats both as "nothing to show". If you must tell them apart, compare against null and undefined separately.
Gotchas
An empty value is not a missing field
A required field can still be null on a record, because required in the wire sense means "must be accessible", not "must have a value". Do not assume that a field in fields always has a value.
Do not list the same field twice
Listing a field in both arrays is redundant and confusing to readers. Pick one place for each field.
Fields must be imported references
Use references from @salesforce/schema, such as ACCOUNT.Name. The imports give you compile-time checking and let the platform track dependencies. Hard-coded strings like 'Account.Name' also work in some forms, but they skip that checking and are easy to misspell. For fields on a related record, import the parent relationship path as a string, such as 'Account.Owner.Name', only when no schema import exists.
Each distinct field list is a separate cache entry
Lightning Data Service caches record data per record. When two components request different field sets for the same record, the service merges what it has and fetches what is missing. If many components each request slightly different fields, you can cause extra server calls. Where practical, let components that show the same record request the same fields.
Required fields cost you the whole read
Because one inaccessible field in fields fails the entire wire, do not add a field to fields only because "it is probably always there". Decide based on what happens to the user when it is not.
Choosing in practice
Walk through a component on an Account record page that shows the name, the industry, and a custom rating field from a managed package.
Name:fields. Every Account has a name, and the component is not useful without it.Industry:optionalFields. Field-level security can hide it, and the component can skip the line.- The package field:
optionalFields. It may not exist in every org.
@wire(getRecord, {
recordId: '$recordId',
fields: [NAME_FIELD],
optionalFields: [INDUSTRY_FIELD, RATING_FIELD]
})
account;Exercises
Exercise
A component lists Account.Name and Account.AnnualRevenue in fields. A support user without field-level access to AnnualRevenue opens the page. What does the wire return for that user, and how would you change the component so the user still sees the name?
Show answer
The wire sets error and provisions no data, because one unreadable field in fields fails the whole request. Move AnnualRevenue to optionalFields and read it with getFieldValue, which returns undefined when the field is omitted. Hide the revenue line in the template when the value is undefined or null.
Exercise
Why can data.fields.Industry.value throw an error when Industry is in optionalFields, and which helper avoids the problem?
Show answer
If the user cannot read Industry, the field is omitted from data.fields. Then data.fields.Industry is undefined, and reading .value on it throws a TypeError. getFieldValue(data, INDUSTRY_FIELD) returns undefined instead of throwing.