Sub topic 7
Saving changes with updateRecord
This chapter covers how a screen action writes changes to a record. It assumes you can read an LWC template and class, use @api properties and event handlers, and deploy a component with Salesforce DX. The previous chapters showed how the panel receives recordId and how it lays out its UI. Here the Save button does its work.
What updateRecord does
updateRecord is a function from the lightning/uiRecordApi module. It sends field changes to the User Interface API and returns a Promise. The platform checks the user's field-level security, sharing, and validation rules, so you need no Apex for a simple edit.
The function takes an object with a fields property. The fields object maps field API names to new values. It must include the Id field, or the call fails.
Building the fields object
Import the field references from @salesforce/schema. Imports give you compile-time checks, and they keep the code working if a field is renamed. Use fieldApiName as the key.
import ID_FIELD from '@salesforce/schema/Account.Id';
import DESCRIPTION_FIELD from '@salesforce/schema/Account.Description';
const fields = {};
fields[ID_FIELD.fieldApiName] = this.recordId;
fields[DESCRIPTION_FIELD.fieldApiName] = this.description;Only send the fields you want to change. Fields you leave out keep their current values.
The complete save handler
The handler below sets a saving flag, calls updateRecord, and reports the outcome. It sits in the same class that holds recordId and description.
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;
}
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;
}
}
}The template binds disabled={isSaving} on the Save button. That stops a second click from sending a duplicate request while the first is in flight.
The save flow
The diagram shows the order of events. The panel closes only on success. On failure it stays open so the user keeps what they typed.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Error about a missing Id | `fields` has no `Id` key | Add `ID_FIELD.fieldApiName` set to `recordId` |
| `recordId` is undefined | Read in the constructor | Use it in handlers or `connectedCallback`, after the platform sets it |
| Insufficient access error | User lacks edit permission on the field | Check field-level security and the profile or permission set |
| Validation rule message | A rule rejected the value | Show `error.body.message` so the user can correct it |
The error object from updateRecord has a body property. For field-level problems, error.body.output.fieldErrors holds details per field. The optional chaining in the example (error?.body?.message) protects against errors that have no body, such as a network failure.
Why not call Apex?
Use updateRecord when you change fields on one record and the standard rules are enough. Use Apex when you must update several related records in one transaction, run logic the user should not control from the client, or work with objects that the User Interface API does not support.
Exercise
Your Save handler calls updateRecord({ fields }) and fails with an error saying the record Id is missing. The fields object contains only Description. What do you change?
Show answer
Add the Id to the fields object: fields[ID_FIELD.fieldApiName] = this.recordId;, with ID_FIELD imported from @salesforce/schema/Account.Id. updateRecord uses the Id entry to know which record to update.
Exercise
Why does the handler set isSaving back to false in a finally block instead of only in the catch block?
Show answer
finally runs after both success and failure, so the Save button is always re-enabled. Putting it only in catch would leave the button disabled after a successful save if the panel stays open or the component is reused.