Sub topic 6
Responding to success, error, submit and cancel
This chapter assumes you can build a basic lightning-record-form (covered earlier) and that you know how LWC event handlers work. Here you will attach handlers to the events the form fires, so your component can react when a save finishes, fails, or is abandoned.
The events at a glance
lightning-record-form fires events at different points in a save. You listen for them with on attributes in the template.
| Event | Attribute | Fires when | Useful data |
|---|---|---|---|
| submit | `onsubmit` | The user clicks Save, before data is sent | `event.detail.fields` |
| success | `onsuccess` | The record was saved | `event.detail.id` |
| error | `onerror` | The save failed | `event.detail.message` |
| cancel | `oncancel` | The user clicks Cancel | None needed |
| load | `onload` | The form finishes loading record data | Record data in `event.detail` |
The order of events
A save follows one of two paths. A successful save fires submit, then success. A rejected save fires submit, then error. Cancel is separate and never reaches the server.
Wiring the handlers
Bind each event in the template. The form below creates an Account, because it has no record-id.
<template>
<lightning-record-form
object-api-name="Account"
fields={fields}
onsuccess={handleSuccess}
onerror={handleError}
oncancel={handleCancel}>
</lightning-record-form>
</template>The class below handles all three events. It shows a toast for success and error, and it clears local state on cancel.
import { LightningElement } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';
export default class AccountCreate extends LightningElement {
fields = [NAME_FIELD, INDUSTRY_FIELD];
savedId;
handleSuccess(event) {
this.savedId = event.detail.id;
this.dispatchEvent(
new ShowToastEvent({
title: 'Account saved',
message: 'Record ID: ' + this.savedId,
variant: 'success'
})
);
}
handleError(event) {
this.dispatchEvent(
new ShowToastEvent({
title: 'Save failed',
message: event.detail.message,
variant: 'error'
})
);
}
handleCancel() {
this.savedId = undefined;
}
}What each handler receives
- Success:
event.detail.idholds the ID of the saved record. For a create, this is the only place you learn the new ID. - Error:
event.detail.messageis a short summary.event.detail.detailmay carry more specific text, so log it while debugging. - Cancel: the event carries no payload. The form handles resetting its own fields, so your handler only needs to update your own state, for example closing a modal.
- Submit:
event.detail.fieldslists the values about to be saved. Inlightning-record-form, treat this event as a place to observe the save, for example to show a spinner. To change values before saving, uselightning-record-edit-form, which is covered in a later chapter.
Common mistakes
- Reading
event.detail.recordIdinstead ofevent.detail.id. The property isid, so the first form returnsundefined. - Forgetting that
ShowToastEventmust be dispatched from the component. It is not a method you call. - Expecting a handler to run when the form is in
readonlymode. That mode has no Save or Cancel buttons, so nosubmit,successorcancelevent can fire.
Exercise
A create form built with lightning-record-form saves successfully. Which event gives you the ID of the new record, and which property holds it?
Show answer
The success event (onsuccess). The new record's ID is in event.detail.id.
Exercise
Your form is in readonly mode and you added oncancel and onsuccess handlers. Why do they never run?
Show answer
Readonly mode shows no Save or Cancel buttons, so the user cannot save or cancel, and neither event fires.