Sub topic 1
What a Quick Action Is and When to Use an LWC
Prerequisites
This chapter assumes you already know the following:
- The basic structure of a Lightning web component (LWC): an HTML template, a JavaScript class, and a
js-meta.xmlfile. - Decorators such as
@api, and how to handle events in a component. - Salesforce record pages and page layouts.
- The basic Salesforce DX project and deployment workflow.
You do not need to have built a quick action before. This chapter gives the working model. Later chapters cover the metadata, the record context, and the code in detail.
What a quick action is
A quick action is a button that a user clicks to do one task without leaving the page they are on. Quick actions appear in the highlights panel of a record page, in the Salesforce mobile app action bar, and in the global actions menu (the "+" icon in the Lightning Experience header).
Salesforce has several kinds of quick action:
- Create a Record opens a form to create a related record.
- Update a Record opens a form to edit the current record.
- Log a Call records a call against the current record.
- Custom actions run something you build yourself. These can be a Lightning component, a Visualforce page, a Flow, or a Lightning web component.
This tutorial covers the last case: using an LWC as the body of a custom action.
Why use an LWC for an action
The built-in action types cover simple cases. They fall short when you need any of the following:
- A custom layout or custom validation that a field-list form cannot express.
- Logic that depends on several fields or related records before it saves.
- An action that does work with no form at all, such as calling an Apex method and reporting the result.
- Calls to external systems, or other behavior that goes beyond saving field values.
An LWC gives you full control of the markup and the JavaScript, and it uses the same component model as the rest of your Lightning code. You can reuse base components, Lightning Data Service wire adapters, and Jest tests.
When another tool is a better fit
An LWC is not always the right choice. Check these alternatives first.
| Requirement | Better choice | Reason |
|---|---|---|
| Create or edit a record with a plain list of fields | Create a Record or Update a Record action | Configured in Setup with no code to maintain |
| Multi-step wizard that admins must change often | Screen Flow action | Admins can edit it without a deployment |
| Custom UI or logic that code must control | LWC action | Full control of markup, validation, and API calls |
| Background work with no user input | Headless LWC action, or a Flow | No panel is needed |
A useful rule: if an admin can build it with a declarative action in a few minutes, do that. Use an LWC when the behavior needs code.
The two types of LWC action
An LWC quick action is either a screen action or a headless action. The type is set in the component's js-meta.xml file.
- A screen action opens a modal panel. The panel contains your component's UI. The user fills it in and clicks a button to finish. The
actionTypevalue isScreenAction. - A headless action has no UI. When the user clicks the action button, the platform calls a public
invoke()method on your component. TheactionTypevalue isAction.
Both types receive the record context, and both use the same toast and refresh tools. The main difference is whether the user needs to see and interact with a panel.
The minimum pieces of a screen action
Every LWC quick action needs the following pieces. Each is covered in its own chapter.
- A
js-meta.xmlfile that exposes the component with thelightning__RecordActiontarget and anactionType. - A JavaScript class with an
@api recordIdproperty, which the platform fills in on a record page. - For a screen action, a template that uses
lightning-quick-action-panelfor the header, body, and footer. - A way to close the panel, which is
CloseActionScreenEventfromlightning/actions. - A quick action record created in Setup, then added to a page layout.
Here is the metadata file for a screen action. It is the smallest piece and shows how the platform recognizes the component as an action.
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>60.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordAction</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__RecordAction">
<actionType>ScreenAction</actionType>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>Use Action in place of ScreenAction for a headless action.
How a click becomes your code
Understanding the order of events helps when you debug.
- The user clicks the action on a record page.
- For a screen action, Salesforce opens a modal panel and creates an instance of your component inside it. For a headless action, Salesforce creates the instance without showing it.
- The platform sets
recordIdon the instance. For a record page, it also providesobjectApiName. - For a screen action, your template renders and the user interacts with it. For a headless action, the platform calls
invoke(). - Your code does its work, then reports the result and closes the panel (screen action only).
A first look at a headless action
A headless action is much shorter than a screen action. It exposes one public method.
import { LightningElement, api } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class NotifyRecord extends LightningElement {
@api recordId;
@api invoke() {
this.dispatchEvent(new ShowToastEvent({
title: 'Action ran',
message: 'Record ' + this.recordId + ' was processed',
variant: 'success'
}));
}
}The platform calls invoke() when the user clicks the action. No template content is needed, though the component still needs an HTML file. An empty <template></template> is enough.
Limits and edge cases to know early
These points often surprise developers who are new to LWC actions.
- The component loads only on the targets you declare. A component with only
lightning__RecordActioncannot be placed on a page in App Builder. Add other targets if you need that, but keep the action component dedicated to its job. recordIdis not set in the constructor. The platform assigns it after the instance exists. Read it in handlers, getters, orconnectedCallback, not in the class constructor.- A headless action's
invoke()can run more than once. In some cases the platform may callinvoke()again if the user clicks the action again. Guard against repeated work, for example with a flag, if the action has side effects. - A global action has no record. Actions created for
lightning__GlobalActiondo not receive arecordId. A later chapter covers them. - Mobile behavior can differ. Test the action in the Salesforce mobile app if your users work there, because panel layout and available features can differ from desktop.
Where this fits in the tutorial
This chapter gives you the vocabulary and the decision rule. The next chapters follow the order of the build:
- The two action types in more detail, then the metadata file.
- The record context that the platform passes in.
- A complete screen action, then its panel layout, saving, closing, and toasts.
- A headless action.
- Creating the action in Setup and adding it to a layout.
- Refreshing the page, global actions, and testing.
Exercise
A sales manager wants a button on the Opportunity page that marks the record as reviewed and shows a success message. No input is needed from the user. Which LWC action type fits, what actionType value would you set in js-meta.xml, and which method does the platform call?
Show answer
A headless action fits, because no input or confirmation is needed. Set actionType to Action under the lightning__RecordAction target. The platform calls the public invoke() method (marked @api) on the component when the user clicks the action.
Exercise
A team needs an Account action that shows a custom form with a text area and a Save button, and an admin could build a simple version of this with the Update a Record action. When is it still reasonable to build an LWC instead?
Show answer
Build an LWC when the form needs behavior that the declarative action cannot express, such as custom validation, logic that depends on other fields or related records, or calls to Apex or external systems. If the form is only a list of fields to edit, the Update a Record action is easier to maintain and an LWC is not needed.