Sub topic 12
Deploying the component and adding it to a page
This chapter covers the last step of the workflow: sending the component bundle to an org and placing it on a Lightning page. It assumes you have already written the accountSummaryCard bundle from the earlier chapters. That means the template, the JavaScript class with its @api properties, and a js-meta.xml file with isExposed, targets, and targetConfigs. It also assumes the Salesforce CLI (sf) is installed and authorized against a sandbox or Developer Edition org, and that you know your way around Setup.
What you are deploying
A Lightning web component is a folder, and App Builder reads the whole folder. For this tutorial the bundle looks like this:
force-app/main/default/lwc/accountSummaryCard/
├── accountSummaryCard.html
├── accountSummaryCard.js
└── accountSummaryCard.js-meta.xmlThe folder name and the file prefixes must match exactly, including case. The component's API name is accountSummaryCard, and App Builder lists it under its masterLabel, "Account Summary Card".
The .js-meta.xml file is the only file App Builder uses to decide whether and where the component appears. The template and class run later, when the page renders.
Deploying with the Salesforce CLI
From the root of your Salesforce DX project, deploy only this bundle:
sf project deploy start --source-dir force-app/main/default/lwc/accountSummaryCardThis targets your default org. To pick a different org, add --target-org with its alias or username:
sf project deploy start --source-dir force-app/main/default/lwc/accountSummaryCard --target-org my-sandboxThe command validates the metadata on the server. If it succeeds, the output lists the component with a state such as Created or Changed. If it fails, the output lists one row per problem, with the file, line, and message.
If you are not sure which org is the default, run sf org list. The default is marked in the output.
You can also deploy from VS Code. Right-click the accountSummaryCard folder and choose SFDX: Deploy Source to Org. It runs the same deployment.
Deploy-time checks on the metadata file
The server checks the metadata file against the JavaScript class. Several mistakes from earlier chapters surface here:
| Problem | Typical cause |
|---|---|
| A property in `targetConfig` is rejected | No `@api` property with that name exists in the class, or the name has a different case. |
| A property type is rejected | The `type` is not one App Builder supports for that target, or it does not fit the `@api` property. |
| A target is rejected | The target name is misspelled, for example `lightning__Recordpage`. |
| The XML cannot be parsed | A tag is unclosed, or the namespace on `LightningComponentBundle` is missing. |
| The API version is rejected | `apiVersion` is higher than the org supports. |
Fix the file and deploy again. A failed deployment does not change the component already in the org.
What happens after a successful deploy
Deployment only stores the bundle in the org. Nothing is added to any page automatically. Admins see the component in App Builder, and only if isExposed is true and at least one target matches the kind of page they are editing.
Adding the component to a page
You can start in two places, depending on the page type.
Opening App Builder
- Record page. Open any Account record. Click the gear icon in the top right and choose Edit Page. App Builder opens with the record page in edit mode.
- App page or Home page. In Setup, search for Lightning App Builder and click New. Choose App Page or Home Page, pick a template, and name the page. To change an existing page, open it from the same list.
Placing the component
- In the left panel, find the Custom section of the components list. Your component appears there under its
masterLabel. You can also type part of the name in the search box. - Drag Account Summary Card onto a region of the canvas.
- Click the component on the canvas. The right panel shows its properties.
- Change values such as Header text. The canvas updates as you type or select.
- Click Save.
The record page template matters. If you work on a record page, the component only appears if the page's object is allowed by the objects element in your targetConfig. In the spine's example, the record page version is limited to Account, so it does not appear when you edit a Contact page.
Which properties show on which page
The property panel shows only the properties listed in the targetConfig that matches the page type. With the metadata from the previous chapter, the panel differs by page:
| Page type | Properties in the panel |
|---|---|
| App page | Header text |
| Home page | Header text |
| Record page (Account) | Header text, Maximum items |
On an app page or home page, maxItems is not configurable. The component uses the default in the JavaScript class, 5. The @api property still exists, but App Builder has no field for it on those pages. If you want admins to set a value on every page type, repeat the property element in each targetConfig.
Record context on a record page
On a record page, App Builder sets recordId and objectApiName for you. You do not declare them in targetConfig, and they never appear in the property panel. The component only needs to declare them with @api, as accountSummaryCard.js already does. On app and home pages these two properties stay undefined, because there is no current record. The template then renders "Record Id:" with an empty value.
If your component must work on all three page types, write code that handles the missing record. For example, wrap record-specific markup in an lwc:if check on recordId.
Saving, activating, and assigning
Saving a page does not make it visible to users. After the first save, App Builder offers Activate.
- A record page can be assigned as the org default for the object, as the default for specific apps, or for a combination of app, record type, and profile.
- An app page is added to a Lightning app's navigation, or reached through a tab you create for it.
- A home page is assigned as the org default or to specific apps and profiles.
You can preview the result without activating. Use Preview in App Builder, or open the page through the Page Properties dialog. Users, however, only see the page once it is activated and assigned to them.
Redeploying after changes
When you edit the component, deploy again with the same command. Then refresh App Builder:
- Reload the App Builder tab with a normal browser refresh. App Builder reads the list of components and their properties when it loads, so a new property or label does not appear in an open session.
- If the page itself still shows old behavior, hard refresh the browser to clear cached resources.
The properties already set on placed components keep their values. New properties with a default appear at that default. New properties with no default appear empty.
Troubleshooting a deployed component
If the deployment succeeded but the component is missing, work through these checks in order.
isExposed. Open the deployed file in the org or in your project. It must contain<isExposed>true</isExposed>.- Target for the page type. An app page needs
lightning__AppPage, a home page needslightning__HomePage, and a record page needslightning__RecordPage. - Object restriction. On a record page, the page's object must be in the
objectslist of the matchingtargetConfig. - Right org. Run
sf org listand compare the default org with the one open in your browser. Deploying to one org and checking another is a common cause. - Stale session. Reload App Builder after deploying.
- Property missing in the panel. The property must be in the
targetConfigfor the current page type, and thenamemust match the@apiproperty exactly.
Exercises
Exercise
You deploy accountSummaryCard with the metadata from this tutorial. An admin opens a Contact record page in App Builder and cannot find the component. The deployment succeeded and isExposed is true. What is the likely cause, and how do you fix it?
Show answer
The record page targetConfig contains <object>Account</object> inside objects, which limits the component to Account record pages. Add <object>Contact</object> to that list, deploy again, and reload App Builder.
Exercise
On a Home page, the admin sees the Header text field but not Maximum items. Is this a bug, and what would you change to show Maximum items there?
Show answer
It is expected. The maxItems property is declared only in the targetConfig for lightning__RecordPage. To show it on Home pages, add the same property element to the targetConfig whose targets includes lightning__HomePage, then deploy and reload App Builder.