Sub topic 5
Building a screen action
This chapter assumes you know the basic structure of a Lightning web component (HTML template, JavaScript class, js-meta.xml), the @api decorator, and how to deploy with a Salesforce DX project. You will build a screen action that lets a user edit an Account description from a record page.
What a screen action does
A screen action is a quick action that opens a modal panel. The user fills in fields, then clicks a button to save or cancel. The platform creates your component when the user clicks the action and destroys it when the panel closes.
The platform sets recordId on the component. You declare it with @api and read it when you save.
Step 1: Expose the component
Add the lightning__RecordAction target and set actionType to ScreenAction. Without this, the component does not appear in the list of components available for a quick 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>Step 2: Build the template
Wrap the content in lightning-quick-action-panel. It supplies the header and the body. Put the buttons in the footer slot.
<template>
<lightning-quick-action-panel header="Update Description">
<lightning-textarea
label="Description"
value={description}
onchange={handleChange}>
</lightning-textarea>
<div slot="footer">
<lightning-button variant="neutral" label="Cancel" onclick={handleCancel}></lightning-button>
<lightning-button variant="brand" label="Save" onclick={handleSave} disabled={isSaving}></lightning-button>
</div>
</lightning-quick-action-panel>
</template>The Save button is disabled while isSaving is true. This prevents a double click from sending two updates.
Step 3: Write the controller
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 class has four parts that matter:
- Record context.
@api recordIdreceives the Id of the record where the user clicked the action. - Saving.
updateRecordneeds afieldsobject that contains theIdand each field to change. The schema imports give you field API names that the compiler checks. - Closing. Dispatching
CloseActionScreenEventfromlightning/actionstells the platform to close the panel. Both Cancel and a successful save use it. - Feedback. On success the component shows a toast and closes. On failure it shows an error toast and leaves the panel open, so the user can retry without losing their text.
Common errors
| Symptom | Likely cause |
|---|---|
| The component is missing from the action's component list | `isExposed` is false, or the `lightning__RecordAction` target or `actionType` is missing |
| The panel does not close after saving | `CloseActionScreenEvent` was not dispatched, or it was imported from the wrong module |
| `recordId` is `undefined` in the constructor | The platform sets it after construction, so read it in a handler or `connectedCallback` |
| Save works but the page shows old values | The record page has not refreshed. A later chapter covers refreshing the view. |
Exercise
Which two parts of the screen action tell the platform to treat the component as a modal action and to close that modal when work is finished?
Show answer
The actionType value ScreenAction in js-meta.xml makes the component a screen action that opens in a modal panel. Dispatching CloseActionScreenEvent from the component closes the panel.
Exercise
The Save handler throws an error from updateRecord. Based on the code above, what does the user see, and why is the panel left open?
Show answer
The user sees an error toast with the message from the error body. The catch block does not dispatch CloseActionScreenEvent, so the panel stays open and the user can retry. The finally block resets isSaving so the Save button works again.