Sub topic 9
Using recordId and objectApiName on record pages
This chapter assumes you can read a Lightning web component bundle (HTML, JavaScript, and a .js-meta.xml file). It also assumes you know what @api does and that you have already exposed a component with lightning__RecordPage as a target.
What the record page supplies
When a component sits on a record page, Lightning Experience knows which record the user is viewing. It passes two values to the component:
recordIdis the 15 or 18 character ID of the current record.objectApiNameis the API name of that record's object, such asAccountorCustom_Thing__c.
The component receives them only if it declares public properties with exactly those names.
import { LightningElement, api } from 'lwc';
export default class AccountSummaryCard extends LightningElement {
@api recordId;
@api objectApiName;
@api headerText = 'Summary';
@api maxItems = 5;
}You do not list recordId or objectApiName in targetConfigs. They are not admin settings. The page sets them at runtime, and they do not appear in the property panel.
Showing the values in the template
The template binds to the two properties like any other field.
<template>
<lightning-card title={headerText} icon-name="standard:account">
<div class="slds-var-p-around_medium">
<p>Record Id: {recordId}</p>
<p>Object: {objectApiName}</p>
<p>Max items: {maxItems}</p>
</div>
</lightning-card>
</template>Deploy the bundle, place it on an Account record page, and the card shows that record's ID and Account.
Where the values are available
The context is set after the component is created, so recordId is undefined inside the constructor(). Use it in connectedCallback() or in a getter instead. If the user navigates between records without a full page reload, the value can change, so code that depends on it should not assume it is set only once.
| Page type | `recordId` | `objectApiName` |
|---|---|---|
| Record page | Set | Set |
| App page | Not set | Not set |
| Home page | Not set | Not set |
The same component can be placed on all three page types if its targets allow it. On app and home pages both properties stay undefined, so a component that serves several targets should handle that case.
get hasRecord() {
return Boolean(this.recordId);
}<template if:true={hasRecord}>
<p>Viewing record {recordId}</p>
</template>
<template if:false={hasRecord}>
<p>Place this component on a record page to see record details.</p>
</template>Restricting the component to certain objects
A record page component with no objects element is available on record pages for every object. To make it available only on Account pages, add the objects element to the record page targetConfig. The component will then be missing from App Builder when an admin edits a Contact or Opportunity page.
<targetConfig targets="lightning__RecordPage">
<objects>
<object>Account</object>
</objects>
<property name="headerText" type="String" label="Header text" default="Summary" />
</targetConfig>Restricting the objects lets your code assume what objectApiName will be, but it is still good practice to read the value instead of hard coding it.
Common mistakes
- Misspelling the property, for example
recordID. JavaScript names are case sensitive, so the page never sets it. - Declaring
recordIdwithout@api. The property is private, and the page cannot set it. - Reading
recordIdin the constructor and gettingundefined. - Expecting the values on an app or home page.
- Adding
recordIdas a<property>intargetConfigs. It is not needed, and the page supplies the value anyway.
Exercises
Exercise
A component declares @api recordId; and is exposed with the targets lightning__RecordPage and lightning__HomePage. An admin places it on the Home page. What is the value of recordId, and how should the component respond?
Show answer
recordId is undefined, because the Home page has no current record. The component should check for a value, for example with a getter such as hasRecord, and show a fallback message or hide record-specific content.
Exercise
You want the component to appear in App Builder only on Contact record pages. Which element do you add, and where?
Show answer
Add <objects><object>Contact</object></objects> inside the targetConfig whose targets attribute is lightning__RecordPage, in the .js-meta.xml file.