Sub topic 2
Screen actions and headless actions
This chapter compares the two kinds of Lightning web component (LWC) quick action. It covers how the platform runs each kind, how to choose between them, and what goes wrong when you pick the wrong one. It assumes you know the basic LWC file structure (HTML template, JavaScript class, js-meta.xml), the @api decorator, and how to dispatch events. It also assumes you have seen a record page and a page layout in Salesforce. Later chapters build each kind in full. Here the goal is a clear mental model.
Two ways to run a component from a button
A quick action is a button on a record page. When the user clicks it, the platform runs your component. There are two ways it can do that.
- A screen action opens a modal dialog and renders your component inside it. The user sees a form, a confirmation, or other UI, and decides when to finish.
- A headless action has no UI. The platform creates your component, sets its properties, and calls a public method named
invoke(). Whatever your code does happens straight away, with no dialog.
You choose the kind in the component's js-meta.xml, not in the component's code. The actionType value is ScreenAction for a screen action and Action for a headless 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>Change ScreenAction to Action and the same bundle becomes a headless action. The next chapter covers the metadata file in detail. For now, remember that this one value decides which contract your component must follow.
What the platform does for each kind
The two kinds differ in who controls the lifecycle. In a screen action, your component controls it. In a headless action, the platform does.
Screen action lifecycle
For a screen action, the platform opens a modal and renders your template inside it. It sets recordId (and objectApiName) on the component. Your component stays on screen until it asks the platform to close it. It asks by dispatching CloseActionScreenEvent, which is imported from lightning/actions. If your code never dispatches that event, the user can only leave by clicking the close icon on the modal.
The standard layout component is lightning-quick-action-panel. It gives you the header, the body, and a footer slot for buttons, so your action looks like the other quick actions on the page.
Headless action lifecycle
For a headless action, nothing is rendered. The platform creates the component, sets the record context, and calls invoke(). The method must be public, which means it needs the @api decorator. If it is missing, the action does nothing when clicked.
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'
}));
}
}There is no modal, so there is nothing to close. A headless action does not dispatch CloseActionScreenEvent. Its only way to talk to the user is a toast, a navigation, or a change to the record that the page then shows.
Comparing the two
| Aspect | Screen action | Headless action |
|---|---|---|
| `actionType` | `ScreenAction` | `Action` |
| Visible UI | Modal dialog with your template | None |
| Entry point | Template renders, user interacts | Platform calls `@api invoke()` |
| Record context | `recordId` and `objectApiName` set by the platform | Same properties, set before `invoke()` |
| How it ends | Dispatch `CloseActionScreenEvent` | Ends when `invoke()` returns |
| Typical use | Forms, confirmations, multi-step input | One-click operations that need no input |
Both kinds receive the same record context. The difference is only in how the work starts and finishes.
Choosing between them
Ask what the user must decide before the action runs.
- Does the user need to enter or confirm anything? If yes, use a screen action. A field edit, a reason for rejection, or a "Are you sure?" prompt all need UI.
- Is the operation fully determined by the record? If yes, a headless action works. Examples are marking a record as reviewed, cloning with fixed values, or calling an Apex method that needs only the record Id.
- Could the operation do damage if clicked by accident? Lean toward a screen action with a confirm step. A headless action runs the moment the button is clicked, with no second chance.
- Is feedback long or detailed? A toast holds a short message. If you must show a list of results or errors, a screen action can display them in the panel.
A headless action is faster for the user because there is no dialog to open and dismiss. That speed is also its main risk, so reserve it for operations that are safe to repeat or easy to undo.
Behavior to expect
Several details trip people up when moving between the two kinds.
- Record context is not guaranteed in early lifecycle code. The platform assigns
recordIdafter it creates the component. Do not read it in the constructor. For a screen action, read it in a handler or after render. For a headless action, read it insideinvoke(). invoke()runs on each click. Treat each call as a fresh request. Do not assume the component was created for this click only, and do not rely on state from a previous call being clear. Reset any fields you use at the start ofinvoke().- No UI means no progress indicator. If a headless action calls the server and takes a few seconds, the user sees nothing happen. A screen action can disable its Save button with an
isSavingflag, as theupdateRecordexample later in this tutorial does. For a slow headless operation, consider whether a screen action with a spinner is the better design. - Errors must be reported explicitly. An unhandled rejection inside
invoke()surfaces nowhere the user can see. Wrap server calls intryandcatch, and show an error toast in thecatchblock. - The wrong
actionTypegives confusing results. A component written withinvoke()but declaredScreenActionopens an empty modal. A component with a full template but declaredActionnever shows that template, because the platform only callsinvoke().
import { LightningElement, api } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class SafeHeadless extends LightningElement {
@api recordId;
@api async invoke() {
try {
await this.doWork(this.recordId);
this.notify('Done', 'The record was processed', 'success');
} catch (error) {
this.notify('Action failed', error?.body?.message || 'Unknown error', 'error');
}
}
async doWork(id) {
// Replace with an imperative Apex call or updateRecord.
return id;
}
notify(title, message, variant) {
this.dispatchEvent(new ShowToastEvent({ title, message, variant }));
}
}The example shows the shape of a safe headless action. Errors from doWork become a visible message. Every outcome ends in a toast.
Setting the action up in Setup
Both kinds are created the same way. In Object Manager, open the object, choose Buttons, Links, and Actions, and create a new action. Set Action Type to Lightning Web Component, then pick your component. Finally add the action to a page layout. A component only appears in that list when its metadata exposes it with lightning__RecordAction and a matching actionType. A later chapter walks through these steps in order.
Summary of the decision
Use a screen action when the user must see, enter, or confirm something. Use a headless action when the record alone defines the work and the operation is safe to run on one click. Declare your choice in js-meta.xml, then follow that kind's contract: a template and CloseActionScreenEvent for screen actions, an @api invoke() method for headless ones.
Exercise
A teammate writes a component with @api invoke() that updates a record and shows a toast, but declares <actionType>ScreenAction</actionType>. When users click the action, they see an empty modal and the record is never updated. What is wrong, and how do you fix it?
Show answer
The actionType does not match the component's contract. With ScreenAction, the platform opens a modal and renders the template. It does not call invoke(), so the update never runs. Because the component has nothing to render, the modal is empty. Change actionType to Action so the platform treats it as a headless action and calls invoke() on click. Then redeploy the bundle.
Exercise
A headless action calls an Apex method that can fail. Name two things the invoke() method should do so the user is not left guessing, and explain why a screen action has an easier time with the same problem.
Show answer
First, wrap the call in try and catch. Second, show a success toast on success and an error toast with the error message in the catch block. Without these, a failure produces no visible result. A screen action has an easier time because its panel can show a spinner, disable the Save button with a flag like isSaving, and display error text inside the modal while it stays open. A headless action has only toasts.