Sub topic 2
Building a Screen Action
This chapter assumes you know basic LWC structure, @api, event handlers, Salesforce record pages, and js-meta.xml. A screen action opens a panel where a user enters values. The component then saves those values and closes the panel.
Build the screen action
The component receives the current record ID through @api recordId. The template uses lightning-quick-action-panel to provide the modal layout.
<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 footer slot places buttons at the bottom of the panel. Disabling Save during the request prevents duplicate submissions.
flowchart TD
A[User opens action] --> B[Panel displays]
B --> C[User edits description]
C --> D[Save calls updateRecord]
D --> E[Show success toast]
E --> F[Close panel]Caption: The normal flow of a screen action. Alt: A flow diagram showing a user opening a panel, editing a description, saving it, receiving a success message, and seeing the panel close.
Save and close the panel
updateRecord is a Lightning Data Service function that updates a record without custom Apex. The fields object maps field API names to values.
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 = {
[ID_FIELD.fieldApiName]: this.recordId,
[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;
}
}
}CloseActionScreenEvent tells Salesforce to dismiss the screen action. Dispatch it for both Cancel and successful Save. Keep it out of the error path so the user can correct the problem.
| Requirement | Implementation |
|---|---|
| Edit a value | `lightning-textarea` |
| Save a record | `updateRecord` |
| Close the panel | `CloseActionScreenEvent` |
Exercise
Why is the Save button disabled while updateRecord is running?
Show answer
It prevents the user from submitting duplicate updates before the first request finishes.