Understanding updateRecord in Lightning Data Service (LDS) for LWC

Understanding updateRecord in Lightning Data Service (LDS)
Chinmaya By Chinmaya
3 Min Read

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

  1. Requires Record ID: You must always include the record Id

  2. Field API Names: Use the full field path (e.g., Account.Name)

  3. Error Handling: Always wrap in try-catch

  4. No Wire Adapter: Unlike getRecord, this is a function you call

Comparison with Similar Methods

MethodTypePurposeReal-time Sync
updateRecordFunctionUpdate records✅ Yes
getRecordWire AdapterRead records✅ Yes
deleteRecordFunctionDelete records✅ Yes

When to use updateRecord vs Apex

Use updateRecord when:

    1. Doing simple field updates

    2. You want automatic UI refresh

    3. No complex validation needed

Use Apex when:

    1. Need complex business logic

    2. Updating related records

    3. Require transaction control

Best Practices

    1. Combine with getRecord: First load data, then update

    2. Refresh UI: Use refreshApex if needed

    3. Handle errors: Always show user feedback

    4. 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.

Share This Article
Follow:
Chinmaya is working as a Senior Consultant with a deep expertise in Salesforce. Holding multiple Salesforce certifications, he is dedicated to designing and implementing cutting-edge CRM solutions. As the creator of Writtee.com, Chinmaya shares his knowledge on educational and technological topics, helping others excel in Salesforce and related domains.
Leave a comment