Sub topic 9
Handling load, success and error events
This chapter assumes you can build a basic lightning-record-edit-form with lightning-input-field components, and that you know how to write LWC event handlers and import ShowToastEvent. Here you will learn which events the form fires and what each one carries. You will also learn how to report save results to the user.
The events at a glance
A lightning-record-edit-form talks to Lightning Data Service. It fires events as the form loads, saves, and fails. You listen with on attributes in the template.
| Event | Fires when | Useful data |
|---|---|---|
| `onload` | The record data and metadata are ready | `event.detail.records` |
| `onsubmit` | The user clicks the submit button | `event.detail.fields` |
| `onsuccess` | The save completed | `event.detail.id`, `event.detail.fields` |
| `onerror` | The save failed | `event.detail.message`, `event.detail.detail`, `event.detail.output.fieldErrors` |
The next chapter covers onsubmit in depth. This chapter focuses on the other three.
Reacting to load
The onload event fires once the form has the record and its field metadata. Use it to run logic that needs the data, such as hiding a spinner. For an existing record, event.detail.records is an object keyed by record ID.
handleLoad(event) {
const records = event.detail.records;
const recordId = Object.keys(records)[0];
const fields = records[recordId].fields;
this.currentName = fields.Name.value;
this.isLoading = false;
}Create forms have no record-id, so there is no existing record to read. Do not rely on event.detail.records holding values there.
Reporting success
When the save works, onsuccess fires. The new or updated record ID is in event.detail.id. The saved values are in event.detail.fields.
import { LightningElement } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class ContactCreate extends LightningElement {
handleSuccess(event) {
const recordId = event.detail.id;
this.dispatchEvent(
new ShowToastEvent({
title: 'Contact created',
message: 'Record ID: ' + recordId,
variant: 'success'
})
);
}
}After a create, the form keeps its entered values. If you want an empty form for the next record, reset the inputs yourself:
this.template
.querySelectorAll('lightning-input-field')
.forEach((field) => field.reset());Handling errors
When the save fails, onerror fires. event.detail.message is a short summary. For validation rules or required fields, event.detail.output.fieldErrors holds per-field messages, and event.detail.detail holds more text.
handleError(event) {
this.dispatchEvent(
new ShowToastEvent({
title: 'Save failed',
message: event.detail.message,
variant: 'error'
})
);
}Add lightning-messages inside the form. It shows server errors in place, so users see them even when you write no handler. If you also show a toast, the user may see the error twice, so choose one approach deliberately.
<lightning-record-edit-form
object-api-name="Contact"
onload={handleLoad}
onsuccess={handleSuccess}
onerror={handleError}>
<lightning-messages></lightning-messages>
<lightning-input-field field-name="LastName"></lightning-input-field>
<lightning-button type="submit" label="Save"></lightning-button>
</lightning-record-edit-form>Common mistakes
- Reading
event.detail.idinonerror. No record was saved, so there is no ID. - Forgetting
type="submit"on the button. Without it, no save starts and no event fires. - Using the value from
onloadas the saved value. It is the value from before the user edited anything.
Exercise
A user clicks Save on a create form and a validation rule rejects the record. Which event fires, and where can you find the message for a specific field?
Show answer
onerror fires. Per-field messages are in event.detail.output.fieldErrors. lightning-messages also shows the error on the form without extra code.