Sub topic 4
Reading a record with getRecord
This chapter assumes you know JavaScript modules and async/await, the structure of a Lightning web component, the @api decorator, and the basics of Salesforce objects, fields, and record IDs. You will wire getRecord to a component, read its result, and see where it fails.
What getRecord does
getRecord is a wire adapter in the lightning/uiRecordApi module. A wire adapter is a function that the wire service calls for you. You give it parameters, and it provisions a result to your component. getRecord returns the data for one record, limited to the fields you ask for.
The data comes from Lightning Data Service (LDS). LDS keeps a shared client-side cache of records. If another component on the page already loaded the same record and fields, your wire reads the cached copy and does not make a new server call.
Wiring getRecord to a property
Import getRecord and the field references. Schema imports from @salesforce/schema give you field objects, so you do not hard-code API names as strings. A misspelled import fails at build time, and a misspelled string fails only at runtime.
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 decorated property account receives an object with two keys, data and error. Only one holds a value at a time.
recordId: '$recordId'makes the parameter reactive. The$prefix tells the wire service to readthis.recordId. When that property changes, the wire runs again. The value is a string, and the property name follows the$.fieldslists fields that must be readable. If the user cannot access one of them, the whole request fails.optionalFieldslists fields that are returned when accessible and skipped silently when not.
The helpers getFieldValue and getFieldDisplayValue pull one field out of the result. getFieldValue returns the raw value. getFieldDisplayValue returns the formatted value, which matters for picklists and currency. Both return undefined when the field is missing, and they accept undefined data, so they are safe to call before the wire has responded.
Rendering the result
The wire provisions data and error asynchronously. The first render happens before either has a value, so the template must handle that.
<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>Wiring to a function
Wire to a function when you need to run code on each result, for example to copy values into separate properties.
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
export default class AccountNameLogger extends LightningElement {
@api recordId;
name;
errorMessage;
@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;
}
}
}The function receives the same { data, error } object each time the wire emits. Clear the value you are not setting, as the example does. Otherwise a stale name stays visible beside a new error.
Common errors
| Symptom | Likely cause |
|---|---|
| Wire never runs | `recordId` is `undefined`, for example on a page without a record context. A reactive parameter that is `undefined` holds the wire back. |
| `error` set, no data | A field in `fields` is not accessible, or the record is not visible to the user. |
| Value is `undefined` | The field was not requested, or it was in `optionalFields` and inaccessible. |
| Wire does not rerun | The parameter lacks the `$` prefix, so it is passed as a fixed string. |
Exercise
A component wires getRecord with fields: [NAMEFIELD, INDUSTRYFIELD]. A user without access to Industry opens the page. What happens, and how do you change the wire so the name still displays?
Show answer
The whole request fails and the wire sets error, because every entry in fields must be accessible. Move INDUSTRY_FIELD to optionalFields. The wire then returns data with Name, and getFieldValue for Industry returns undefined.
Exercise
What does the "$" in recordId: '$recordId' do, and what happens without it?
Show answer
It marks the parameter as reactive, so the wire reads this.recordId and reruns when it changes. Without the "$", the literal string "recordId" is sent as the ID, and the request fails.