A screen quick action appears in a modal window. Provide your own markup or use the lightning-quick-action-panel component for a consistent user interface based on Lightning Design System.
Unlike other Lightning web components on record pages, LWC quick actions don’t pass in recordId through connectedCallback(). To capture recordId when the framework assigns it, declare an @api setter that stores the value in a private field. In your template and JavaScript, reference this._recordId to read the record ID.
1_recordId;2@api set recordId(value){3 this._recordId = value;4}
Open and Close the Modal Window
A screen quick action opens a Lightning web component in a modal window. To close the modal window programmatically, for example, to create a Cancel button, build UI that dispatches the custom event CloseActionScreenEvent. Import the event from the lightning/actions module.
The following sections contain complete code samples.
If you build a screen quick action with custom footer buttons, pressing X only closes the modal, there are no hooks to execute additional logic on close. If a screen quick action has logic that executes on Cancel, the logic is bypassed when the panel closes.
Note
Use lightning-quick-action-panel for a Consistent UI
To provide a consistent Salesforce UI, wrap your Lightning web component in a lightning-quick-action-panel component, which provides a header, body, and footer consistent with the modal blueprint in the Salesforce Lightning Design System.
1<template>2 <lightning-quick-action-panel header="My action">3 Here's some content for the modal body.45 <div slot="footer">6 <lightning-button variant="neutral" label="Cancel"></lightning-button>7 <lightning-button variant="brand" label="Save" class="slds-m-left_x-small"></lightning-button>8 </div>9 </lightning-quick-action-panel>10</template>
Create a Form in the Modal Body
One way to create the modal body is to use the lightning-record-edit-form component with field values populated by lightning-input-field components. The cancel and submit buttons must be nested within the lightning-record-edit-form component, so with this approach the footer slot isn’t needed.
This example creates a form that populates the name and phone fields on the lightning-record-edit-form without a footer. It renders a modal window with a header containing the text Edit Fields Action.
When a user clicks the submit button, the handleSuccess event handler is called. The handler closes the modal window using the CloseActionScreenEvent function.
1import{LightningElement, api}from "lwc";2import{ShowToastEvent}from "lightning/platformShowToastEvent";3import{CloseActionScreenEvent}from "lightning/actions";45export default class QuickEditFormExample extends LightningElement{6 @api recordId;7 @api objectApiName;89 handleSuccess(e){10 // Close the modal window and display a success toast11 this.dispatchEvent(new CloseActionScreenEvent());12 this.dispatchEvent(13 new ShowToastEvent({14 title: "Success",15 message: "Record updated!",16 variant: "success",17}),18);19}20}
Create a Custom Form with Buttons in the Footer
You can create a form in the modal body using lightning-input and lightning-button components. With this approach, use the lightning-quick-action-panel component’s footer slot to contain the buttons.
This example creates a form for a quick action on a contact record similar to the previous example, and uses buttons in the footer because it doesn’t use a record form component. The field displays the initial values using the getRecord wire adapter.
The Save button is disabled if the Last Name field is blank. Clicking the Save button closes the modal window and displays a toast if the save is successful. To save your record changes, call updateRecord(recordInput, clientOptions).
1import{LightningElement, api, wire}from "lwc";2import{getRecord, getFieldValue}from "lightning/uiRecordApi";3import{updateRecord}from "lightning/uiRecordApi";4import{CloseActionScreenEvent}from "lightning/actions";5import{ShowToastEvent}from "lightning/platformShowToastEvent";6import FNAME_FIELD from "@salesforce/schema/Contact.FirstName";7import LNAME_FIELD from "@salesforce/schema/Contact.LastName";8import PHONE_FIELD from "@salesforce/schema/Contact.Phone";910const FIELDS = [FNAME_FIELD, LNAME_FIELD, PHONE_FIELD];1112export default class QuickEditExample extends LightningElement{13 disabled = false;14 @api recordId;15 @api objectApiName;1617 @wire(getRecord, {recordId: "$recordId", fields: FIELDS})18 contact;1920 get firstname(){21 return getFieldValue(this.contact.data, FNAME_FIELD);22}2324 get lastname(){25 return getFieldValue(this.contact.data, LNAME_FIELD);26}2728 get phone(){29 return getFieldValue(this.contact.data, PHONE_FIELD);30}3132 handleCancel(event){33 // Add your cancel button implementation here34 this.dispatchEvent(new CloseActionScreenEvent());35}3637 handleLastNameChange(event){38 // Display field-level errors if last name field is empty.39 if(!event.target.value){40 event.target.reportValidity();41 this.disabled = true;42}else{43 this.disabled = false;44}45}4647 handleSubmit(e){48 // Add your updateRecord implementation4950 // Close the modal window and display a success toast51 this.dispatchEvent(new CloseActionScreenEvent());52 this.dispatchEvent(53 new ShowToastEvent({54 title: "Success",55 message: "Record updated!",56 variant: "success",57}),58);59}60}
Get Information about the Page
You can use standard LWC features to get information about the current page, including a page reference from the navigation service, the record ID, and the object API name of the current record.
This example displays the record ID and object API name of the current record. It also returns the current page reference, which describes the current page and its state.
1<template>2 <p>These two fields are auto-populated based on the record context:</p>3 <p>RecordId: <i>{recordId}</i>, objectApiName: <i>{objectApiName}</i></p>4 <p>{pageRefString}</p>5</template>
1import{LightningElement, api, wire}from "lwc";2import{CurrentPageReference}from "lightning/navigation";34export default class RecordContextAction extends LightningElement{5 @api recordId;6 @api objectApiName;78 @wire(CurrentPageReference)9 pageRef;1011 get pageRefString(){12 return JSON.stringify(this.pageRef);13}14}