Sub topic 6
Choosing fields or optionalFields
This chapter assumes you can write a basic Lightning web component and wire getRecord with a reactive $recordId. It also assumes you know how to import field references from @salesforce/schema. Here you decide which fields go in fields and which go in optionalFields.
Two ways to request a field
The getRecord adapter accepts two lists of field references.
fieldsholds fields the component cannot work without. If any of them is missing or the user cannot read it, the whole request fails and the wire returns anerror.optionalFieldsholds fields the component can live without. If a field here is missing or hidden from the user, the request still succeeds. The field is left out of the result.
The difference shows up only when something goes wrong. When everything is accessible, both lists return the same data.
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 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 getFieldValue(this.account.data, INDUSTRY_FIELD);
}
}In this component, Name is required. Industry is optional, so a user without field-level access to Industry still sees the record.
How a missing field behaves
Suppose a user cannot read Industry.
- If
Industryis infields, the wire returns an error andaccount.datais undefined. The component shows nothing useful. - If
Industryis inoptionalFields, the wire returns data without that field.getFieldValuereturnsundefinedfor it.
A decision rule
Ask one question for each field: can the component still do its job without it?
| Situation | Use |
|---|---|
| The component is meaningless without the value | `fields` |
| The value is extra detail or a nice-to-have | `optionalFields` |
| The field may not exist in every org, such as a field from a managed package | `optionalFields` |
| Field-level security may hide the field from some profiles | `optionalFields` |
Many developers put every field in optionalFields so the component never fails. That choice has a cost. A component that renders with a blank where a key value should be can mislead users. A clear error is often better than a silent gap.
Common mistakes
- Putting every field in
fields. One restricted field breaks the whole component for that user. - Forgetting that optional fields can be missing. Code that assumes
getFieldValuealways returns a value can show blanks or fail on later logic. Check forundefinedwhere it matters. - Listing the same field in both lists. Pick one list for each field.
- Using string API names with the wrong format. Prefer imported schema references. A plain string must be written as
Account.Industry, with the object name first.
Guarding the template
Because optional values can be absent, make the template tolerate them.
<template>
<template lwc:if={account.data}>
<p>Name: {name}</p>
<template lwc:if={industry}>
<p>Industry: {industry}</p>
</template>
</template>
</template>The Industry line is shown only when a value exists, so users who cannot read it see no empty label.
Exercise
A component shows an Opportunity's Name and a custom field Partner__c from a managed package that is not installed in every org. Which list should each field go in, and why?
Show answer
Put Name in fields, because the component cannot work without it. Put Partner_c in optionalFields, because in orgs without the package the field does not exist. With fields, the request would fail in those orgs. With optionalFields, the wire still returns the record, and the template can hide the Partner_c line when the value is undefined.
Exercise
A user lacks read access to a field listed in fields. What does the wire return, and what does the component's data property contain?
Show answer
The wire returns an error, and the data property is undefined. The whole request fails, not just that one field.