Sub topic 9
Telling the user what happened with toasts
This chapter assumes you can build a basic LWC, use @api and event handlers, and have a screen action that saves a record with updateRecord. Here you add feedback so the user knows whether the save worked.
Why feedback matters
When a screen action closes, the user sees the record page again. Nothing says whether the save worked. A toast is a short message that appears at the top of the page and fades away. It fills that gap.
A headless action has no panel at all, so a toast is often its only visible output.
Dispatching a toast
You create a ShowToastEvent and dispatch it from your component. Import it from lightning/platformShowToastEvent.
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
this.dispatchEvent(new ShowToastEvent({
title: 'Success',
message: 'Description updated',
variant: 'success'
}));The event takes these main properties:
| Property | Purpose |
|---|---|
| `title` | Bold heading of the toast. |
| `message` | Body text. |
| `variant` | One of `info` (default), `success`, `warning`, or `error`. |
| `mode` | One of `dismissible` (default), `pester`, or `sticky`. |
The variant sets the color and icon. The mode sets how long the toast stays. A dismissible toast disappears after a few seconds and has a close button. A sticky toast stays until the user closes it, so use it for errors the user must read.
Toasts in the screen action
Here is the save handler from the screen action. It shows a success toast and closes the panel. If the save fails, it shows an error toast and leaves the panel open so the user can retry.
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;
}
}Note three details:
- The success toast is dispatched before the close event. The toast belongs to the page, not to the panel, so it stays visible after the panel closes.
- The
catchblock does not close the panel. Closing it would discard what the user typed. finallyresetsisSaving, so the Save button works again after an error.
Reading the error message
Errors from lightning/uiRecordApi carry details in error.body. The message property holds a readable summary, such as a validation rule message. Optional chaining (error?.body?.message) protects you when the error is not in that shape, for example a network failure. The fallback text 'Unknown error' then appears instead of undefined.
Toasts in a headless action
A headless action runs invoke() and has no UI to show a result. The toast is how you report it.
@api invoke() {
this.dispatchEvent(new ShowToastEvent({
title: 'Action ran',
message: 'Record ' + this.recordId + ' was processed',
variant: 'success'
}));
}The flow of feedback
Common mistakes
- Showing a success toast before
await updateRecordfinishes. The user sees "Success" even if the save later fails. - Using
variant: 'error'with the defaultdismissiblemode for important failures. The user may miss it. Considersticky. - Forgetting to import
ShowToastEvent. The component then fails with a reference error at runtime, only when the handler runs.
Exercise
In the screen action's catch block, why does the code show an error toast but not dispatch CloseActionScreenEvent?
Show answer
Closing the panel would throw away the text the user typed and hide the form. Leaving it open lets the user read the error toast, fix the problem, and click Save again. The finally block re-enables the Save button for that retry.
Exercise
You want a headless action to warn the user and keep the message on screen until they dismiss it. Which two ShowToastEvent properties do you set, and to what values?
Show answer
Set variant to warning and mode to sticky.