Sub topic 14
When to use base record form components instead
This chapter covers the last decision in the tutorial. You have getRecord, updateRecord, and getRecordNotifyChange. Before you write JavaScript for every record screen, you need to know when the base record form components already do the job.
It assumes you know the following:
- The
@wiredecorator and thegetRecordadapter. - The
recordIdproperty on record pages. - Promises and
async/await. - Basic Salesforce objects and fields.
What the base record form components are
Lightning base components include three record forms. All of them use Lightning Data Service (LDS), so they share the same client-side cache as getRecord and updateRecord.
| Component | What it does | Typical use |
|---|---|---|
| `lightning-record-form` | Shows a whole form from a list of fields or a layout. It can be read-only or editable, and it includes Save and Cancel buttons. | Quick form with little customization |
| `lightning-record-view-form` | Shows fields read-only. You place each `lightning-output-field` yourself. | Custom read-only layout |
| `lightning-record-edit-form` | Shows editable fields. You place each `lightning-input-field` yourself and control the submit button. | Custom edit layout with validation and hooks |
The components load the record, apply field-level security and the user's permissions, format values for the user's locale, and render the right input for each field type. They also save changes and report errors. With uiRecordApi you write all of that yourself.
A form with no JavaScript
This component edits an account's name and industry. It has no wire, no imports, and no save handler.
<template>
<lightning-record-edit-form
record-id={recordId}
object-api-name="Account"
onsuccess={handleSuccess}
>
<lightning-messages></lightning-messages>
<lightning-input-field field-name="Name"></lightning-input-field>
<lightning-input-field field-name="Industry"></lightning-input-field>
<lightning-button type="submit" label="Save" variant="brand"></lightning-button>
</lightning-record-edit-form>
</template>import { LightningElement, api } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
export default class AccountQuickEdit extends LightningElement {
@api recordId;
handleSuccess() {
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'Account updated',
variant: 'success'
})
);
}
}The form loads the record, fills the inputs, saves on submit, and shows errors in lightning-messages. The only JavaScript is the toast on success.
The read-only version uses lightning-record-view-form with lightning-output-field. For the fastest option, lightning-record-form takes a fields array and a mode.
<template>
<lightning-record-form
record-id={recordId}
object-api-name="Account"
fields={fields}
mode="view"
></lightning-record-form>
</template>import { LightningElement, api } from 'lwc';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';
export default class AccountQuickView extends LightningElement {
@api recordId;
fields = [NAME_FIELD, INDUSTRY_FIELD];
}In view mode, lightning-record-form shows an edit icon next to fields the user can edit. Clicking it switches the form to edit mode with Save and Cancel buttons.
What the forms do for you
The forms handle several things that you would otherwise build with uiRecordApi:
- Field-level security. Fields the user cannot read are not shown. You do not need
optionalFieldsto avoid failures. - Input types. A picklist field gets a picklist, a date gets a date picker, and a lookup gets a lookup. With
getRecordyou pick the input component for each field. - Required fields and validation. The form shows required markers and reports validation rule errors.
- Cache updates. After a save, wired
getRecordresults for the same record in other components refresh. - Layout.
lightning-record-formcan load a page layout withlayout-type, so admins change the form without a code change.
When uiRecordApi is the better choice
Forms are less flexible than code. Choose getRecord and updateRecord when one of these applies:
- You need the value, not a field. A component computes a badge, a chart, or a calculated message from record data. Forms only render fields.
- The UI is not a form. Cards, headers, tiles, and custom lists display values in their own markup.
- You change data without user input. A button sets a status field, or a component updates a record after another action finishes.
updateRecordtakes the field values directly. - You need full control of the save flow. You want to change values just before saving, save several records in sequence, or run extra logic after the result.
- You need to react to data changes in code. A wired function receives every new result, so the component can respond to it.
You can also combine the two. A lightning-record-view-form can show the record while a wire on the same record feeds a computed property. Both read from the same cache, so they stay in agreement.
Decision guide
Start from the simplest tool that meets the requirement. Move to uiRecordApi when the form components cannot express what the screen needs.
Edge cases and trade-offs
- Customizing a record-edit-form. You can set values in the
onsubmithandler. Callevent.preventDefault(), copyevent.detail.fields, change them, and callthis.refs.form.submit(fields)orthis.template.querySelector('lightning-record-edit-form').submit(fields). This covers many cases that would otherwise push you toupdateRecord. - Fields outside the form. A
lightning-record-edit-formsaves only thelightning-input-fieldcomponents inside it. A plainlightning-inputis ignored on submit unless you add its value in theonsubmithandler. - Cost of two data paths. Mixing forms and wires in one component works, but each path has its own error handling. Keep the error display in one place so users see one message.
- Updating from a form after external changes. If an Apex method changes the record, forms in the same view are not refreshed by themselves. Call
getRecordNotifyChangeas you learned earlier, and the forms refresh too. - Objects the forms do not support. Some objects and field types are not supported by LDS-based components. When you hit a limit, check the documentation for that object. A form that shows no value is a sign to check support before you debug your code.
Summary
- Base record form components use LDS, so they share the cache with
getRecordandupdateRecord. - They handle loading, field-level security, input types, validation, saving, and error messages.
lightning-record-formis the shortest option. The view and edit forms give layout control.- Use
uiRecordApiwhen you need values in code, a non-form UI, or custom save logic. - You can mix both approaches on one record because they read from the same cache.
Exercise
A component must show an account's Name and Industry in a custom two-column layout and let the user edit them with a Save button. It needs no computed values. Which approach fits best, and why would you not choose getRecord with updateRecord?
Show answer
Use lightning-record-edit-form with lightning-input-field elements placed in your own two-column markup and a submit button. It loads the record, renders the correct inputs, respects field-level security, saves, and shows errors. With getRecord and updateRecord you would write the wire, the inputs, the change handlers, the save logic, and the error display yourself, with no extra capability for this requirement.
Exercise
A "Close Case" button must set a Status field to "Closed" and show a toast, with no fields visible to the user. Which approach fits, and which function saves the change?
Show answer
Use uiRecordApi. No form is shown, so a base record form has nothing to render. Call updateRecord with a record input containing the Id and the Status field, then dispatch the toast in the try block, or a failure toast in the catch block.
Exercise
An Apex method changes a field on the record that is shown by a lightning-record-view-form. The form still shows the old value. What do you call, and with what argument?
Show answer
Call getRecordNotifyChange with an array of record identifiers, for example getRecordNotifyChange([{ recordId: this.recordId }]), after the Apex call finishes. This marks the cached record as stale, so the form and any wired getRecord results refresh.