Sub topic 8
Closing the Action Panel
This chapter covers how a screen action closes its own modal panel. It assumes you know the basic LWC file structure (template, class, js-meta.xml), the @api decorator, and how to dispatch events. It also assumes you have already built a screen action that exposes lightning__RecordAction with actionType set to ScreenAction.
Why the component has to close itself
The platform opens the quick action modal when the user clicks the action. It does not know when your work is finished. Your component must tell the platform to close the panel, or the panel stays open after a save.
The platform gives you one standard way to do this: the CloseActionScreenEvent event from the lightning/actions module. You dispatch it from your component, and the platform removes the modal.
import { CloseActionScreenEvent } from 'lightning/actions';
this.dispatchEvent(new CloseActionScreenEvent());The event takes no arguments. It does not bubble or cross the shadow boundary on your behalf, so dispatch it from the root component of the action, using this.dispatchEvent.
When to close
A screen action has three common exit points:
| Exit point | What triggers it | What to do |
|---|---|---|
| Cancel | The user clicks Cancel | Close without saving |
| Success | `updateRecord` resolves | Show a success toast, then close |
| Failure | `updateRecord` rejects | Show an error toast and leave the panel open |
Leaving the panel open on failure is deliberate. The user keeps what they typed and can fix the problem or retry. The isSaving flag in the example prevents a second click while the request is running.
Wiring it into the component
The Cancel button calls a handler that only dispatches the close event. The Save handler dispatches it after the save succeeds.
import { LightningElement, api } from 'lwc';
import { CloseActionScreenEvent } from 'lightning/actions';
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 UpdateDescription extends LightningElement {
@api recordId;
description = '';
isSaving = false;
handleChange(event) {
this.description = event.target.value;
}
handleCancel() {
this.dispatchEvent(new CloseActionScreenEvent());
}
async handleSave() {
this.isSaving = true;
const fields = {};
fields[ID_FIELD.fieldApiName] = this.recordId;
fields[DESCRIPTION_FIELD.fieldApiName] = this.description;
try {
await updateRecord({ fields });
this.dispatchEvent(new ShowToastEvent({
title: 'Success',
message: 'Description updated',
variant: 'success'
}));
this.dispatchEvent(new CloseActionScreenEvent());
} catch (error) {
this.dispatchEvent(new ShowToastEvent({
title: 'Error updating record',
message: error?.body?.message || 'Unknown error',
variant: 'error'
}));
} finally {
this.isSaving = false;
}
}
}The success path dispatches the toast first and the close event second. The toast is owned by the page, not the modal, so it stays visible after the panel closes.
The sequence
Common mistakes
- Closing before the save finishes. If you dispatch the close event without awaiting
updateRecord, the panel can disappear while the save is still running, and an error will never be seen. Alwaysawaitfirst. - Using the event in a headless action. A headless action has no panel. It runs
invoke()and finishes, so there is nothing to close. - Closing in a child component without the event reaching the root. The event must be dispatched from the action's own component. If a child component handles the Save, have it fire a custom event and let the root dispatch the close event.
- Closing on error. This hides the error message context and discards the user's input.
Try it
Run the example in a scratch org. Click Save with text entered and confirm that the toast appears and the panel closes. Then change the field API name to one that does not exist, deploy, and confirm that the panel stays open and shows an error toast.
Exercise
A screen action calls updateRecord inside a try block and dispatches CloseActionScreenEvent in the catch block as well, so the panel always closes. What problem does this cause for the user, and where should the close event go instead?
Show answer
When the save fails, the panel closes immediately and the user loses their input and cannot retry. The close event belongs in the Cancel handler and in the success path, after await updateRecord(...) resolves. In the catch block, show an error toast and leave the panel open.
Exercise
Which module do you import CloseActionScreenEvent from, and does a headless action need it?
Show answer
Import it from lightning/actions. A headless action does not need it, because it has no panel to close.