Sub topic 2
Screen Actions and Headless Actions
This chapter assumes you can read a basic Lightning web component (HTML template, JavaScript class, js-meta.xml), use @api and event handlers, and that you know what a record page and page layout are. You will learn the two kinds of LWC quick action and how to pick between them.
Two ways an action can behave
A quick action is a button on a record page that runs your component. When the user clicks it, the platform does one of two things, depending on the action type you declare.
- A screen action opens a modal panel that shows your component's template. The user reads, types, and clicks buttons inside it.
- A headless action has no panel and no template content. The platform calls a public method on your component, and your code does the work.
You declare the type in js-meta.xml, using ScreenAction or Action as the actionType value. The next chapter covers that file in detail.
Screen actions
Use a screen action when the user must provide input or confirm something before work happens. Examples are editing a field, filling a short form, or reviewing a summary before approving.
A screen action component:
- Renders a template, usually wrapped in
lightning-quick-action-panel. - Receives
recordIdfrom the platform through an@apiproperty. - Closes itself by dispatching
CloseActionScreenEvent, which you import fromlightning/actions.
The platform does not add Save or Cancel buttons for you in this model. You place them in the panel's footer slot and wire them to your own handlers.
Here is the skeleton of a screen action's JavaScript:
import { LightningElement, api } from 'lwc';
import { CloseActionScreenEvent } from 'lightning/actions';
export default class ScreenSkeleton extends LightningElement {
@api recordId;
handleCancel() {
this.dispatchEvent(new CloseActionScreenEvent());
}
}Headless actions
Use a headless action when the work needs no input. Examples are stamping a record, starting a process, or showing a confirmation toast.
A headless action component:
- Exposes a public
invoke()method with@api. - Is called by the platform each time the user clicks the action.
- Does not need a visible template, so the HTML file can hold an empty
<template></template>.
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'
}));
}
}Because there is no panel, nothing needs closing. The method runs, and the action is finished.
Choosing between them
| Question | Screen action | Headless action |
|---|---|---|
| Does the user enter or confirm anything? | Yes | No |
| Is there a visible panel? | Yes, a modal | No |
| Entry point | Template renders, then your handlers run | Platform calls `invoke()` |
| How it ends | You dispatch `CloseActionScreenEvent` | The method returns |
| `actionType` value | `ScreenAction` | `Action` |
A practical rule: start with a headless action if the click alone carries all the intent. Choose a screen action as soon as you need a field, a choice, or a chance to cancel.
Common errors at this stage
- Wrong
actionTypefor the code. AScreenActioncomponent with no template content shows an empty modal. AnActioncomponent withoutinvoke()does nothing when clicked. - Forgetting
@apioninvoke(). Without it, the platform cannot call the method. - Expecting
recordIdin the constructor. The platform sets@api recordIdafter the component is created, so read it in handlers orconnectedCallback, not in the constructor.
Exercise
A team wants an action that sets a record's Status to "Reviewed" in one click, with no input from the user. Which action type fits, which method must the component expose, and what happens if you omit @api from that method?
Show answer
A headless action (actionType of Action) fits, because no input is needed. The component must expose invoke(). Without @api, the method is private to the component, so the platform cannot call it and clicking the action does nothing.