createRecord, updateRecord, and deleteRecord
By the end of this lesson, you'll be able to:
- Use createRecord to insert a new record from JavaScript
- Use updateRecord and deleteRecord to modify and remove records
- Handle success and failure for these Promise-based operations
Prerequisites: "The Wire Service: @wire, getRecord, and getFieldValue"
The LDS Write Functions
import { createRecord, updateRecord, deleteRecord } from 'lightning/uiRecordApi';
All three are Promise-based — Module 4's async/await and try/catch apply directly to working with them.
createRecord
import CONTACT_OBJECT from '@salesforce/schema/Contact';
import FIRST_NAME_FIELD from '@salesforce/schema/Contact.FirstName';
import LAST_NAME_FIELD from '@salesforce/schema/Contact.LastName';
async handleCreateContact() {
const recordInput = {
apiName: CONTACT_OBJECT.objectApiName,
fields: {
[FIRST_NAME_FIELD.fieldApiName]: this.firstName,
[LAST_NAME_FIELD.fieldApiName]: this.lastName,
},
};
try {
const newContact = await createRecord(recordInput);
this.newContactId = newContact.id;
} catch (error) {
this.error = error;
}
}
updateRecord and deleteRecord
async handleUpdateContact() {
const fields = { Id: this.recordId, [LAST_NAME_FIELD.fieldApiName]: this.newLastName };
await updateRecord({ fields });
}
async handleDeleteContact() {
await deleteRecord(this.recordId);
}
updateRecord requires the record's Id inside the fields object; deleteRecord just needs the record ID directly.
Why This Matters in Real Projects
Full CRUD without Apex is genuinely possible for straightforward cases — reducing backend code, deployment surface, and test-class maintenance for components that don't need complex server-side logic.
Exercise
Write the fields object needed to update a Contact's Email, given its recordId and a new email value.
Show hint
Include the Id and the field to update.
Exercise
Challenge: explain, as a comment, why these three functions being Promise-based matters for how you write the surrounding code.
Show hint
Recall Module 4's Promises lesson.
createRecord, updateRecord, and deleteRecord Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
Lightning Data Service's imperative write functions cover full record CRUD without Apex — each one returns a Promise, so Module 4's async/await applies directly.