Introduction
updateRecord is a crucial function in Lightning Data Service (LDS) that allows you to update Salesforce records without writing Apex code.
While it’s not a wire adapter like getRecord
, it’s equally important in the LDS toolkit.
Key Features of updateRecord
✅ No Apex required – Pure client-side operation
✅ Automatic security checks – Respects FLS and CRUD permissions
✅ Optimized performance – Minimizes network calls
✅ Real-time sync – Updates all components using the same record
How to Use updateRecord
Basic Syntax
import { updateRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
// 1. Prepare fields to update
const fields = {};
fields['Id'] = recordId; // Required
fields['Account.Name'] = 'New Account Name';
// 2. Execute update
try {
await updateRecord({ fields });
// Show success message
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'Record updated',
variant: 'success'
})
);
} catch (error) {
// Handle error
}
Complete Example: Updating an Account
import { LightningElement, api } from 'lwc';
import { updateRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import NAME_FIELD from '@salesforce/schema/Account.Name';
export default class UpdateAccount extends LightningElement {
@api recordId;
accountName;
handleNameChange(event) {
this.accountName = event.target.value;
}
async handleUpdate() {
const fields = {};
fields['Id'] = this.recordId;
fields[NAME_FIELD.fieldApiName] = this.accountName;
try {
await updateRecord({ fields });
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'Account updated successfully',
variant: 'success'
})
);
} catch (error) {
this.dispatchEvent(
new ShowToastEvent({
title: 'Error updating record',
message: error.body.message,
variant: 'error'
})
);
}
}
}
Important Notes About updateRecord
Requires Record ID: You must always include the record Id
Field API Names: Use the full field path (e.g.,
Account.Name
)Error Handling: Always wrap in try-catch
No Wire Adapter: Unlike
getRecord
, this is a function you call
Comparison with Similar Methods
Method | Type | Purpose | Real-time Sync |
---|---|---|---|
updateRecord | Function | Update records | ✅ Yes |
getRecord | Wire Adapter | Read records | ✅ Yes |
deleteRecord | Function | Delete records | ✅ Yes |
When to use updateRecord vs Apex
Use updateRecord when:
Doing simple field updates
You want automatic UI refresh
No complex validation needed
Use Apex when:
Need complex business logic
Updating related records
Require transaction control
Best Practices
Combine with
getRecord
: First load data, then updateRefresh UI: Use
refreshApex
if neededHandle errors: Always show user feedback
Validate data: Check values before saving
updateRecord is one of the most powerful tools in LDS, enabling efficient record updates while maintaining security and performance. It’s perfect for most basic to intermediate update scenarios in your Lightning Web Components.