Sub topic 10
Limiting a record page component to specific objects
This chapter assumes you can already write a basic component bundle and edit its .js-meta.xml file. You should also know how targets, targetConfigs and @api properties fit together. Here you will use the objects element to control which objects' record pages can host your component.
Why restrict by object
A component that has lightning__RecordPage in its targets can be placed on the record page of any object. That is usually wrong for components built around one object's fields or business rules. An admin who drops an "Account Summary Card" onto an Opportunity page gets a component that cannot do its job.
The objects element moves that decision from runtime into configuration. App Builder only offers the component on the record pages of the objects you list. The admin never sees it in the component list for other objects, so the mistake cannot happen.
The objects element
You put objects inside a targetConfig that covers lightning__RecordPage. Each object child holds the API name of one object.
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<masterLabel>Account Summary Card</masterLabel>
<description>Shows a titled card with the current record context.</description>
<targets>
<target>lightning__AppPage</target>
<target>lightning__HomePage</target>
<target>lightning__RecordPage</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__AppPage,lightning__HomePage">
<property name="headerText" type="String" label="Header text" default="Summary" />
</targetConfig>
<targetConfig targets="lightning__RecordPage">
<objects>
<object>Account</object>
</objects>
<property name="headerText" type="String" label="Header text" default="Summary" />
<property name="maxItems" type="Integer" label="Maximum items" min="1" max="10" default="5" />
</targetConfig>
</targetConfigs>
</LightningComponentBundle>Three things to notice:
- The
objectselement sits in thetargetConfigforlightning__RecordPageonly. The App Page and Home Page configuration has no object concept, so it has noobjectselement. - The component still declares all three targets. The restriction narrows only where the record page version can go.
- Properties in the same
targetConfigapply to the same pages. ThemaxItemsproperty is therefore offered only on Account record pages in this file.
How the restriction is evaluated
The following diagram shows what App Builder considers when an admin edits a page and looks for your component.
Two consequences follow from this flow:
- Leaving out
objectsmeans "all objects". It does not mean "no objects". objectsis an extra filter on top ofisExposedandtargets. It cannot make a component visible if either of those already hides it.
Listing several objects
Add one object element per object. Standard objects use their plain API name. Custom objects end in __c. Objects from a managed package include the namespace prefix.
<targetConfig targets="lightning__RecordPage">
<objects>
<object>Account</object>
<object>Contact</object>
<object>Invoice__c</object>
<object>acme__Project__c</object>
</objects>
<property name="headerText" type="String" label="Header text" default="Summary" />
</targetConfig>The values must be API names, not labels. "Service Contract" does not work, but ServiceContract does. If you are unsure of a name, check it in Object Manager in Setup, where the API name appears next to the label.
| Object | Value in the `object` element |
|---|---|
| Account | `Account` |
| Opportunity | `Opportunity` |
| Case | `Case` |
| Custom object "Invoice" | `Invoice__c` |
| Packaged custom object "Project" in namespace `acme` | `acme__Project__c` |
A misspelled or non-existent object name is a deployment-time problem. Deploy to a scratch org or sandbox first and read the error output instead of assuming the file is valid because it is well-formed XML.
Pairing the restriction with the JavaScript
The objects filter controls where the component can be placed. It does not stop code from running with unexpected values. A component can still be reached in other ways, for example when someone reuses it as a child of another component, so a component that depends on one object should still behave sensibly.
The component below reads objectApiName and shows a message when the object is not the one it expects.
import { LightningElement, api } from 'lwc';
export default class AccountSummaryCard extends LightningElement {
@api recordId;
@api objectApiName;
@api headerText = 'Summary';
@api maxItems = 5;
get isSupportedObject() {
return this.objectApiName === 'Account';
}
}<template>
<lightning-card title={headerText} icon-name="standard:account">
<div class="slds-var-p-around_medium">
<template lwc:if={isSupportedObject}>
<p>Record Id: {recordId}</p>
<p>Max items: {maxItems}</p>
</template>
<template lwc:else>
<p>This component is designed for Account records.</p>
</template>
</div>
</lightning-card>
</template>Keep the two layers separate in your mind. The objects element is a configuration-time filter that helps admins. The getter is a runtime guard that protects users. You often want both, but the guard is optional if the component is only ever placed through App Builder.
Do not hardcode when the logic is generic
If the component works on any object, do not restrict it just to be tidy. Components that take objectApiName and recordId and pass them to lightning-record-form or to Apex generically are good candidates for no objects element at all. Restrict only when the component assumes fields, record types or behavior specific to named objects.
Deploying and verifying the restriction
Deploy the bundle:
sf project deploy start --source-dir force-app/main/default/lwc/accountSummaryCardThen test both the positive and the negative case:
- Open an Account record page and choose Edit Page. Search the Custom component list for "Account Summary Card". It should be there.
- Open a Contact record page in App Builder. With the configuration above, the component should not appear in the list.
- Open an App Page or Home Page. It should still appear, because the restriction applies only to record pages.
The negative test matters as much as the positive one. A typo in an object name can look the same as a working restriction until you check an object that is supposed to be excluded.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Component missing on an object's record page | The object is not in the `objects` list, or the name does not match its API name | Add the object, using its exact API name |
| Component shows on every object's record page | The `targetConfig` has no `objects` element, or it is in the wrong `targetConfig` | Add `objects` to the config whose `targets` includes `lightning__RecordPage` |
| Property panel lacks a property on one page type | The property is declared in a different `targetConfig` | Declare it in each `targetConfig` that needs it |
| Deployment error mentioning a property | A `property` name does not match an `@api` property in the JavaScript | Make the XML `name` identical to the `@api` property name, including case |
| Restriction seems ignored after a change | The old version is still deployed or the page was not reloaded | Redeploy, then reload App Builder |
If you narrow the list on a component that is already placed on pages, treat it as a change that may affect those pages. Test the change in a sandbox before you push it to production.
Summary
objectsgoes inside atargetConfigthat coverslightning__RecordPage, with oneobjectchild per allowed object.- Values are API names. Custom objects end in
__c, and packaged objects carry a namespace prefix. - No
objectselement means the component is available on record pages of every object. - The restriction affects only record pages. App Pages and Home Pages are unaffected.
- The
objectselement is configuration only. UseobjectApiNamein JavaScript if the component needs a runtime guard.
Exercise
A component lists lightning__AppPage, lightning__HomePage and lightning__RecordPage in its targets. Its only targetConfig is <targetConfig targets="lightning__RecordPage"> with <objects><object>Opportunity</object></objects>. Where can an admin place it, and what must you change so that it can also be used on Case record pages?
Show answer
The admin can place it on App Pages and Home Pages (the restriction does not apply to them) and on Opportunity record pages only. To allow Case record pages, add <object>Case</object> to the same objects list.
Exercise
You wrote <object>Invoice</object> for a custom object whose label is "Invoice", and the component never appears on that object's record pages. What is the most likely cause, and what should the value be?
Show answer
Custom objects must be referenced by API name, which ends in __c. The value should be Invoice__c (with a namespace prefix such as acme__Invoice__c if the object comes from a managed package).