The Wire Service: @wire, getRecord, and getFieldValue
By the end of this lesson, you'll be able to:
- Explain the difference between declarative (@wire) and imperative data access
- Use @wire with getRecord to read a record's fields
- Use getFieldValue to safely extract a specific field from wire data
Prerequisites: "Base Record Components"
What Is the Wire Service?
The wire service is a declarative way to read data — you describe what you need, and LWC automatically handles fetching it, including refetching when relevant inputs change (Lesson 5 covers this in depth). This is in contrast to an imperative call (Module 7), where your code explicitly decides exactly when to make the request.
@wire with getRecord
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, INDUSTRY_FIELD] })
account;
}
Each field is imported from @salesforce/schema/... — a safe, compile-time-checked reference to a real field, rather than a plain string that could contain a typo undetected until runtime.
getFieldValue
import { getFieldValue } from 'lightning/uiRecordApi';
get accountName() {
return getFieldValue(this.account.data, NAME_FIELD);
}
The raw wire result has a somewhat deeply-nested shape. getFieldValue safely extracts a specific field's value, and correctly returns undefined rather than throwing while the data is still loading.
Why This Matters in Real Projects
@wire with getRecord is genuinely the most common way LWC components read Salesforce record data — understanding it well pays off across almost every component you'll build from here on.
Exercise
Write the @wire declaration to fetch a Contact's Email field, given a reactive recordId property.
Show hint
Import getRecord and the schema field, then use @wire(getRecord, {...}).
Exercise
Challenge: explain, as a comment, why getFieldValue is preferred over directly reaching into this.account.data.fields.Name.value.
Show hint
Think about what happens before the data has finished loading.
The Wire Service: @wire, getRecord, and getFieldValue Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
@wire is the declarative, most common way LWC components read Salesforce data — you describe what you need, and the framework handles when and how to fetch it.