Unlike other Lightning web components on record pages, LWC quick actions don’t pass in recordId in connectedCallback(). If you need access to recordId, set the value of recordId in your code.
In your Lightning web component, always expose invoke() as a public method for headless quick actions. The invoke() method executes every time the quick action is triggered.
1import{LightningElement, api}from "lwc";23export default class HeadlessSimple extends LightningElement{4 @api invoke(){5 console.log("Hi, I'm an action.");6}7}
Create an empty template for your Lightning web component.
1<template> </template>
To prevent the quick action from being executed multiple times in parallel in long-running actions, add an internal boolean flag.
The return type of invoke() is void. Returning a Promise makes your method asynchronous, but the returned Promise is ignored.
This code uses a boolean flag to block a double execution and a Promise to wait for the sleep to finish. Even though the return type is void, the code executes asynchronously.
You can dispatch a custom event from a quick action. This example dispatches two toasts sequentially using the event provided by the lightning/platformShowToastEvent module.
1import{LightningElement, api}from 'lwc';2import{ShowToastEvent}from 'lightning/platformShowToastEvent';34export default class DispatchEventHeadlessAction extends LightningElement{5 @api recordId;6 @api async invoke(){7 // Fire Toast message8 let event = new ShowToastEvent({9 title: 'I am a headless action!',10 message: 'Hi there! Starting...'11});12 this.dispatchEvent(event);13 // Wait and fire another another Toast message14 await this.sleep(2000);15 // Fire Toast message16 event = new ShowToastEvent({17 title: 'I am a headless action on record with id ' + this.recordId,18 message: 'All done!'19});20 this.dispatchEvent(event);21}2223 sleep(ms){24 // eslint-disable-next-line @lwc/lwc/no-async-operation25 return new Promise((resolve)=> setTimeout(resolve, ms));26}27}