# How do you perform imperative CRUD operations on records using lightning/uiRecordApi functions?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, uiRecordApi, Imperative CRUD

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-you-perform-imperative-crud-operations-on-records-using-lightning-uirecordapi-functions

## Answer

**Basics:** Use them for simple create, edit or delete actions in a component, such as a button that creates a Task. They return Promises. `createRecord({ apiName, fields })` resolves with the new record (including `id`), `updateRecord({ fields: { Id, ... } })` resolves with the updated record, and `deleteRecord(recordId)` resolves with no value.

**How it works:** Build `fields` with schema-imported field names (`NAME_FIELD.fieldApiName`) to stay refactor-safe. For updates the `Id` field is required in `fields`. The call runs through UI API on the server in its own transaction, then the LDS cache is updated so wired components on the page refresh automatically. Errors are rejected Promises with `body.output.errors` and `body.output.fieldErrors`.

**Advanced / deep dive:** Each call is one record and one transaction; there is no multi-record atomic save, so for batches or parent-child inserts that must succeed together use Apex (with `Database` methods and a savepoint if needed). `updateRecord` accepts a second `clientOptions` argument such as `ifUnmodifiedSince` to detect conflicting edits. Helper functions `generateRecordInputForCreate`, `generateRecordInputForUpdate` and `createRecordInputFilteredByEditedFields` build inputs from `getRecordUi` or `getRecordCreateDefaults`. UI API supports a subset of objects and field types, so some objects (and some fields) are not available. Triggers, flows, validation rules and duplicate rules still run server-side, so errors from them surface as field or page errors. Choose LDS for simple UI CRUD (no Apex test needed, cache-aware) and Apex for logic, bulk, callouts or system-mode needs, remembering that Apex with `without sharing` bypasses sharing deliberately.

**Code / config example:**
```javascript
import { createRecord, updateRecord, deleteRecord } from 'lightning/uiRecordApi';
import TASK from '@salesforce/schema/Task';
import SUBJECT from '@salesforce/schema/Task.Subject';
import WHATID from '@salesforce/schema/Task.WhatId';

async createFollowUp() {
    const fields = { [SUBJECT.fieldApiName]: 'Follow up', [WHATID.fieldApiName]: this.recordId };
    const rec = await createRecord({ apiName: TASK.objectApiName, fields });
    this.taskId = rec.id;
}
async rename(id) {
    await updateRecord({ fields: { Id: id, Subject: 'Updated' } });
}
async remove(id) {
    await deleteRecord(id);
}
```

**Hidden gotchas & must-remember points:**
- `Id` is mandatory in `updateRecord` fields.
- Only one record per call; there is no bulk or transaction across calls.
- Validation rules and triggers run; failures reject the Promise.
- Not all objects and fields are supported by UI API.
- The user needs object, field and record access; sharing is enforced.
- Wrap calls in `try/catch` and surface `body.output.fieldErrors`.

**Best practice:** Use LDS CRUD for single-record UI operations and move multi-record or transactional logic to bulkified Apex.

---
Source: ForceChallenges · last updated 2026-11-02
