Sub topic 10
Limiting a record page component to specific objects
This chapter assumes you can read a .js-meta.xml file and know what isExposed, targets and targetConfigs do. It also assumes you can deploy a component with the Salesforce CLI. Here you will limit a record page component to the objects where it makes sense.
Why restrict by object
A component with lightning__RecordPage in its targets can be placed on a record page for any object. That is often wrong. A card that shows Account fields has no use on an Opportunity page, and an admin who adds it there gets a broken or empty component. The objects element lets you declare which objects a component supports, so App Builder only offers it on matching pages.
The objects element
Put objects inside the targetConfig that applies to lightning__RecordPage. Each object child holds an object API name.
<?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>
<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>
<object>Contact</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>With this file, the component appears on Account and Contact record pages only. On app and home pages it is unaffected, because those targets use a separate targetConfig with no objects element.
Use API names, not labels. Standard objects use their plain name, such as Account or Opportunity. Custom objects end in __c, for example Invoice__c.
How it fits with record context
The restriction pairs with the context properties from the previous chapter. On a record page, Lightning fills in recordId and objectApiName for any component that declares them with @api. Restricting the objects tells you which values objectApiName can take, so your code can rely on them.
import { LightningElement, api } from 'lwc';
export default class AccountSummaryCard extends LightningElement {
@api recordId;
@api objectApiName;
@api headerText = 'Summary';
@api maxItems = 5;
}Deploy and check
Deploy the bundle, then open a record page in App Builder.
sf project deploy start --source-dir force-app/main/default/lwc/accountSummaryCardTest both sides. Open an Account record page and confirm the component is listed under Custom components. Then open an Opportunity record page and confirm it is missing.
Common errors
- Component missing on an object you listed. Check the API name for typos, and for custom objects check the
__csuffix. - Component still appears on every object. The
objectselement may sit in the wrongtargetConfig, or thetargetConfigmay list the wrong targets. The restriction only takes effect inside a config forlightning__RecordPage. - Component missing everywhere. Check
isExposedfirst, then the targets. An invalidobjectvalue can also make the deployment fail, so read the deploy output. - Placed on a page, then restricted later. Existing placements are not a reliable test. Always confirm behavior on a fresh page.
Exercise
Your component has the target lightning_RecordPage and should appear only on Opportunity and a custom object named Invoice_c. Which element do you add, and where?
Show answer
Add an objects element containing two object elements, Opportunity and Invoice_c, inside the targetConfig whose targets attribute includes lightning_RecordPage. Without it, the component is offered on every object's record page.
Exercise
After deploying, the component shows on Account pages but not on Contact pages, although Contact is in your objects list. Name two things to check.
Show answer
Check that the object API name is spelled correctly in the metadata file, and that the deployment actually succeeded and updated the file in the org. Also confirm you are looking at a Contact record page in App Builder, not a different page type.