Sub topic 10
Building a Headless Action
This chapter assumes you can build a basic Lightning web component (HTML template, JavaScript class, js-meta.xml). It also assumes you know @api, event handling, and how a record page uses page layouts. You should have seen how a screen action is exposed and how toasts work in earlier chapters.
What a headless action is
A headless action runs code when the user clicks the action button. It opens no panel. The user sees only what your code produces, such as a toast, a navigation, or a refreshed record.
Use a headless action when the work needs no input. Examples are stamping a record, calling an Apex method with fixed arguments, or navigating to another page. If you need to collect input, build a screen action instead.
Configure the metadata
The metadata is the same as for a screen action, except for actionType. Set it to 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>Action</actionType>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>If you leave ScreenAction here, the platform opens an empty panel and never calls your method.
Write the component
A headless action needs a public method named invoke(). The platform calls it each time the user clicks the action. The component also receives recordId, just as a screen action does.
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 HTML template can be empty, or you can leave out the HTML file entirely. Nothing renders, so a template adds nothing.
How the platform runs it
The platform creates the component, sets recordId, and then calls invoke(). The sequence below shows one click.
Do real work in invoke()
A toast alone is not very useful. Most headless actions change data. You can reuse updateRecord from earlier chapters inside invoke().
Keep these rules in mind:
invoke()may beasync. Handle errors withtryandcatch, and show an error toast on failure, because there is no panel to display a message.- Do not dispatch
CloseActionScreenEvent. There is no panel to close. - The platform can call
invoke()again if the user clicks twice. Guard against duplicate work with a flag such asisRunning. - Do not rely on
connectedCallback()to do the work. The platform may create the component before the click, so put the action ininvoke().
import { LightningElement, api } from 'lwc';
import { updateRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import ID_FIELD from '@salesforce/schema/Account.Id';
import DESCRIPTION_FIELD from '@salesforce/schema/Account.Description';
export default class StampRecord extends LightningElement {
@api recordId;
isRunning = false;
@api async invoke() {
if (this.isRunning) {
return;
}
this.isRunning = true;
const fields = {};
fields[ID_FIELD.fieldApiName] = this.recordId;
fields[DESCRIPTION_FIELD.fieldApiName] = 'Reviewed ' + new Date().toISOString();
try {
await updateRecord({ fields });
this.dispatchEvent(new ShowToastEvent({
title: 'Success',
message: 'Record stamped',
variant: 'success'
}));
} catch (error) {
this.dispatchEvent(new ShowToastEvent({
title: 'Error updating record',
message: error?.body?.message || 'Unknown error',
variant: 'error'
}));
} finally {
this.isRunning = false;
}
}
}Common errors
| Symptom | Likely cause |
|---|---|
| Empty panel opens and nothing happens | `actionType` is `ScreenAction` instead of `Action` |
| Click does nothing | `invoke()` is missing the `@api` decorator or is misspelled |
| `recordId` is `undefined` | The action is a global action, which has no record context |
| No feedback after a failure | The error is not caught and no error toast is shown |
Exercise
A teammate sets actionType to Action and writes a component with a method named invoke() that has no decorator. The action does nothing when clicked. What is the fix?
Show answer
Add the @api decorator, so the method is @api invoke(). The platform can only call public methods, and @api makes invoke() public.
Exercise
Why should a headless action's invoke() not dispatch CloseActionScreenEvent?
Show answer
A headless action opens no panel, so there is nothing to close. Report the result with a toast instead.