Sub topic 11
Creating the action and adding it to a page layout
A component that is exposed with lightning__RecordAction does nothing until you create a quick action that points to it and place that action on a layout. This chapter covers both steps. You will do them in Setup first, then as metadata in a Salesforce DX project.
This chapter assumes you have:
- the
updateDescriptionandnotifyRecordcomponents from earlier chapters, with theirjs-meta.xmlfiles - a Salesforce DX project connected to a scratch org or sandbox
- a working knowledge of page layouts and Lightning record pages
What you are creating
The component and the quick action are separate pieces of metadata.
- The component is code. It lives in the
lwcfolder and declares what it can be used for. - The quick action is a small record on an object. It has a label, a type, and a reference to one component.
- The page layout lists which actions appear on a record page for users assigned to that layout.
The user clicks an action in the layout, the action points to the component, and the platform instantiates it. The platform sets recordId only on that last step.
Creating the action in Setup
Deploy the component to the org first. The action form only lists components that are already there and that expose the right target.
- Open Setup and go to Object Manager.
- Select the object, here Account.
- Choose Buttons, Links, and Actions, then New Action.
- Set Action Type to Lightning Web Component.
- In Lightning Web Component, select your component, for example
c:updateDescription. - Enter a Label. This is the text users see, such as "Update Description".
- Review the Name. Setup generates it from the label. It becomes the API name, and you cannot change it after saving.
- Optionally set an icon on the action detail page, then click Save.
You do not set a height or width. LWC screen actions open in the standard quick action modal, and the panel decides the size.
When the component is not in the list
The picker filters components. If yours is missing, check these in order:
- The component is deployed to this org, not just present in your local project.
isExposedistruein thejs-meta.xml.- The
lightning__RecordActiontarget is listed. - The
targetConfigfor that target setsactionTypetoScreenActionorAction.
The picker can show a component for a different action type than you intended. Setup does not compare the component's actionType with how you plan to use it. A screen action will still run if you pick it, but a mismatch between the code and the meta file causes the failures described later.
Adding the action to a page layout
A new action appears nowhere until a layout includes it.
- In Object Manager, open the object and choose Page Layouts.
- Open the layout your users have, for example Account Layout.
- Find the section named Salesforce Mobile and Lightning Experience Actions.
- If you see a link that says "override the predefined actions", click it. Until you do, the layout uses a default set and you cannot edit the list.
- Drag your action from the palette into this section.
- Save the layout.
Quick actions can also appear in the Salesforce Classic Publisher section. LWC actions are for Lightning Experience and the Salesforce mobile app, so use the Lightning section.
The order of actions in the section controls the order on the page. Actions near the top appear as buttons in the highlights panel. Others fall into the overflow menu, so put your most-used action first.
Layout assignment matters
A layout only affects users whose profile, or record type and profile combination, is assigned to it. After you edit the layout, check Page Layout Assignment to confirm that your test user's profile uses it. A common cause of "the button is missing" is editing the correct layout for the wrong profile.
Lightning record pages
A Lightning record page contains a Highlights Panel component. By default, it reads its actions from the page layout. Some record pages are set to use a custom action list instead. If your action is on the layout but still does not appear, open the page in the Lightning App Builder. Select the Highlights Panel and check whether it uses the layout actions or a custom list.
Creating the action as metadata
Setup works for a first try. A team should keep both pieces in source control. In a Salesforce DX project the quick action is a file in the quickActions folder. The file name is the object API name, a dot, the action name, and the suffix .quickAction-meta.xml.
<?xml version="1.0" encoding="UTF-8"?>
<QuickAction xmlns="http://soap.sforce.com/2006/04/metadata">
<label>Update Description</label>
<optionsCreateFeedItem>false</optionsCreateFeedItem>
<type>LightningWebComponent</type>
<lightningWebComponent>updateDescription</lightningWebComponent>
</QuickAction>Save this as force-app/main/default/quickActions/Account.UpdateDescription.quickAction-meta.xml. Three points matter here:
typemust beLightningWebComponent.lightningWebComponentholds the component name. If your org has a namespace, write it asnamespace:componentName. Without a namespace, write only the name, as shown above.- The file name carries the object and the action name. The action's full name is
Account.UpdateDescription.
The layout is also metadata. Open force-app/main/default/layouts/Account-Account Layout.layout-meta.xml and add the action to the platformActionList or quickActionList. For Lightning Experience, use this block:
<quickActionList>
<quickActionListItems>
<quickActionName>UpdateDescription</quickActionName>
</quickActionListItems>
</quickActionList>In a layout file, an object-specific action is written with its short name, UpdateDescription, not Account.UpdateDescription. Global actions use their plain name as well.
If the layout file already has a quickActionList, add a new quickActionListItems entry inside it. Do not add a second quickActionList element. The file keeps the other sections of the layout, so retrieve it first and edit the retrieved copy.
Deploying in a safe order
Metadata that points to other metadata fails if the target is missing. Deploy together, or in this order:
- The component bundle.
- The quick action.
- The layout.
A single command handles the order for you:
sf project deploy start \
--source-dir force-app/main/default/lwc/updateDescription \
--source-dir force-app/main/default/quickActions \
--source-dir force-app/main/default/layoutsThe deploy computes dependencies within one request, so the action can reference the component in the same deploy. Separate deploys need the order above.
Choosing between Setup and metadata
| Approach | Good for | Cost |
|---|---|---|
| Setup UI | Trying an idea in a scratch org, checking how it looks | Changes are not in source control unless you retrieve them |
| Metadata files | Teams, repeatable deploys, multiple orgs | Layout files are large and merge conflicts are common |
A practical workflow is to build in a scratch org with Setup, then run sf project retrieve start for the action and the layout, and commit the result.
Common problems when creating the action
| Symptom | Likely cause |
|---|---|
| Component is missing from the picker | Not deployed, `isExposed` is `false`, or the target is missing |
| Action exists but no button | Not added to the layout, or the user's profile has a different layout |
| Button shows, nothing opens or an error appears | Wrong `actionType`, or the component throws on load |
| `recordId` is `undefined` | The component was used outside a record page, such as a global action |
| Deploy error naming the component | Namespace prefix wrong in `lightningWebComponent` |
| Component appears for the wrong object | The action was created on another object than the one you tested |
The record-based actions in this tutorial import fields like Account.Description. If you create the same action on Contact, the save will fail because the field does not exist there. Write one component per object, or read objectApiName and build the fields dynamically.
Verifying the result
Open an Account record as a user who has the layout. You should see "Update Description" in the highlights panel or the overflow menu. Click it. A modal opens with the panel header, the text area, and the Cancel and Save buttons. If the action is headless, nothing opens. The toast from invoke() appears instead.
If the button is there but the modal is empty, open the browser console. A missing field import or a typo in a template event handler shows up there.
Exercise
You deployed the component and created the quick action on Account. Users on the "Sales Profile" report that the button does not appear on records, while you see it as an administrator. Name two things to check, in order.
Show answer
First, check Page Layout Assignment to see which layout the Sales profile uses, and confirm the action was added to that layout and not only to the one you use. Second, open the record page in Lightning App Builder and check whether the Highlights Panel uses a custom action list instead of the layout actions.
Exercise
In a layout metadata file, what name do you put in quickActionName for the action stored in Account.UpdateDescription.quickAction-meta.xml, and which type value does the action file need for an LWC?
Show answer
Use UpdateDescription, the short name without the object prefix. The action file needs <type>LightningWebComponent</type>, together with a lightningWebComponent element that names the component.