Sub topic 4
Getting the record context
This chapter assumes you can read a basic LWC bundle (template, class, js-meta.xml) and know how @api properties work. It also assumes you have already exposed a component with the lightning__RecordAction target, as covered in the previous chapter.
How the platform passes context
A quick action on a record page needs to know which record the user clicked it on. You don't query for this. The platform sets two public properties on your component when the action opens:
| Property | Type | Contents |
|---|---|---|
| `recordId` | String | The 15- or 18-character ID of the current record |
| `objectApiName` | String | The API name of the record's object, such as `Account` or `Invoice__c` |
Both properties only exist if you declare them with @api. The platform doesn't add them for you. If you leave out the declaration, the values are silently dropped and your component sees nothing.
Declaring the properties
Declare both properties as public fields in the component class:
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
export default class ShowContext extends LightningElement {
@api recordId;
@api objectApiName;
@wire(getRecord, { recordId: '$recordId', fields: [NAME_FIELD] })
account;
get accountName() {
return this.account.data ? getFieldValue(this.account.data, NAME_FIELD) : '';
}
}The $recordId string makes the wire reactive. The adapter waits until recordId has a value, then provisions account. This is the safest way to use the ID, because you never have to decide when the value has arrived.
A template to display the values:
<template>
<lightning-quick-action-panel header="Record context">
<p>Object: {objectApiName}</p>
<p>Record ID: {recordId}</p>
<p>Name: {accountName}</p>
</lightning-quick-action-panel>
</template>The getRecord wire only works for the field you import. The example is tied to Account. For a component reused on several objects, pass objectApiName into your own logic and avoid hard-coded field imports where possible.
When the values are available
The platform assigns recordId after it constructs the component. That has two consequences:
- Reading
this.recordIdin theconstructorreturnsundefined. - Code that needs the ID should run in a wire, in a setter, or in a handler triggered by the user, such as
handleSave.
If you must react to the first assignment, use a setter:
import { LightningElement, api } from 'lwc';
export default class ContextSetter extends LightningElement {
_recordId;
@api
get recordId() {
return this._recordId;
}
set recordId(value) {
this._recordId = value;
// Safe place to start work that depends on the ID
}
}Common errors
recordIdis undefined in a global action. Global actions (lightning__GlobalAction) aren't tied to a record, so the platform has nothing to pass. Ask the user to choose a record, or use a record action instead.- Missing
@api. The field exists but staysundefined. Check the decorator first when context is empty. - Reading the ID too early. Constructor access fails, as described above.
- Hard-coding the object. Importing
Account.Nameand then adding the action to a Contact layout gives a wire error. Match the imports to the objects that carry the action. - Assuming the ID is the same format everywhere. Record pages normally give an 18-character ID, but don't compare IDs with plain string equality against 15-character values from other sources.
Exercise
A teammate adds a quick action component to the Contact layout, but this.recordId is undefined inside connectedCallback, and the component displays nothing. The class contains recordId; with no decorator. What is the fix, and why is a reactive wire a better place to use the ID than the constructor?
Show answer
Declare the field as @api recordId; so the platform can set it. The platform assigns recordId after the component is constructed, so the constructor can't see it. A wire configured with recordId: '$recordId' waits until the value exists and re-runs if it changes, so the code never reads it too early.