Sub topic 13
Showing row and table errors
This chapter assumes you have read the earlier chapters on key-field, inline editing, and draft values. You should also be comfortable with updateRecord and refreshApex.
The errors attribute
When a save fails, the table should show where and why. lightning-datatable has an errors attribute for this. It takes one object with two optional properties, rows and table.
| Property | Shape | What the user sees |
|---|---|---|
| `rows` | Object keyed by the row's key-field value. Each entry has `title`, `messages`, and `fieldNames`. | An error icon on the row. The cells named in `fieldNames` get a red border. |
| `table` | Object with `title` and `messages`. | A summary message for the whole table, shown in the footer with the Save and Cancel buttons. |
The keys in rows must match the values of the column you named in key-field. In this tutorial that is the Contact Id. Each name in fieldNames must match a column's fieldName. If either one is wrong, the table shows no error and does not warn you.
Binding errors in the template
Add errors next to the draft-values binding from the editing chapter:
<lightning-datatable
key-field="Id"
data={data}
columns={columns}
draft-values={draftValues}
errors={errors}
onsave={handleSave}
hide-checkbox-column>
</lightning-datatable>Building errors from a failed save
The earlier handleSave used Promise.all. That call rejects on the first failure and tells you nothing about the other rows. To report every row that failed, use Promise.allSettled. It waits for all saves and returns one result per draft, in the same order.
import { updateRecord } from 'lightning/uiRecordApi';
import { refreshApex } from '@salesforce/apex';
errors = {};
draftValues = [];
async handleSave(event) {
const drafts = event.detail.draftValues;
const results = await Promise.allSettled(
drafts.map((draft) => updateRecord({ fields: { ...draft } }))
);
const rows = {};
results.forEach((result, index) => {
if (result.status !== 'rejected') {
return;
}
const body = result.reason?.body ?? {};
const fieldErrors = body.output?.fieldErrors ?? {};
const messages = Object.values(fieldErrors)
.flat()
.map((fieldError) => fieldError.message);
rows[drafts[index].Id] = {
title: 'We could not save this row.',
messages: messages.length ? messages : [body.message ?? 'Unknown error.'],
fieldNames: Object.keys(fieldErrors)
};
});
const failedIds = Object.keys(rows);
if (failedIds.length === 0) {
this.errors = {};
this.draftValues = [];
} else {
this.errors = {
rows,
table: {
title: `${failedIds.length} row(s) could not be saved.`,
messages: ['Fix the highlighted cells and save again.']
}
};
this.draftValues = drafts.filter((draft) => rows[draft.Id]);
}
await refreshApex(this.wiredResult);
}Three details matter here:
fieldErrorsis an object that maps a field API name to an array of error objects.Object.values(...).flat()collects every message.- Only the failed drafts stay in
draftValues. Rows that saved are no longer edited, so the table removes their highlighting. - Setting
errorsto{}on success clears every message. If you skip this step, old errors stay visible.
Common mistakes
- Mistyping a field in
fieldNames. The row error appears, but no cell is highlighted. - Clearing
draftValueswhen saves failed. The user's edits vanish and the error messages have nothing to point at. - Passing an array to
errorsinstead of an object withrowsandtable.
Exercise
A save fails for a Contact whose Id is 003xx0000012345. The failing field is Email. What must the errors object contain so the Email cell of that row is highlighted?
Show answer
errors.rows must have a key "003xx0000012345" whose value includes fieldNames: ['Email'], plus a title and a messages array. The key must equal the key-field value, and 'Email' must equal the column's fieldName.