Skip to main content
lightning-record-form is the simplest way to create, edit, or view a record using Lightning Data Service. It automatically generates the form layout based on the object’s metadata and handles data loading, saving, and validation.

Usage

Static Field Definition

Import field schema references for type safety and IDE support:
import { LightningElement, api } from 'lwc';

import ACCOUNT_FIELD from '@salesforce/schema/Contact.AccountId';
import NAME_FIELD from '@salesforce/schema/Contact.Name';
import TITLE_FIELD from '@salesforce/schema/Contact.Title';
import PHONE_FIELD from '@salesforce/schema/Contact.Phone';
import EMAIL_FIELD from '@salesforce/schema/Contact.Email';

export default class RecordFormStaticContact extends LightningElement {
    @api recordId;
    @api objectApiName;

    fields = [ACCOUNT_FIELD, NAME_FIELD, TITLE_FIELD, PHONE_FIELD, EMAIL_FIELD];
}
<template>
    <lightning-record-form
        object-api-name={objectApiName}
        record-id={recordId}
        fields={fields}
    ></lightning-record-form>
</template>

Dynamic Field Definition

Specify fields as strings for more flexible runtime configuration:
import { LightningElement, api } from 'lwc';

export default class RecordFormDynamicContact extends LightningElement {
    @api recordId;
    @api objectApiName;
    fields = ['AccountId', 'Name', 'Title', 'Phone', 'Email'];
}
<template>
    <lightning-record-form
        object-api-name={objectApiName}
        record-id={recordId}
        fields={fields}
    ></lightning-record-form>
</template>

Attributes

AttributeTypeRequiredDescription
object-api-nameStringYesAPI name of the object (e.g., ‘Contact’, ‘Account’)
record-idStringNoID of the record to load. Omit to create a new record
fieldsString[] or FieldId[]NoArray of field API names or field schema references
layout-typeStringNoLayout to use: ‘Full’ (default) or ‘Compact’
modeStringNoForm mode: ‘view’, ‘edit’, or ‘readonly’
columnsNumberNoNumber of columns (1 or 2, default is 2)
record-type-idStringNoRecord type ID for record creation

Features

  • Automatic Layout: Generates form layout based on object metadata
  • Built-in Validation: Handles field-level and record-level validation
  • Save/Cancel: Includes save and cancel buttons automatically
  • Lightning Data Service: Uses LDS for caching and offline support
  • Responsive: Adapts to different screen sizes
When using lightning-record-form, you cannot customize the layout or add custom buttons. For more control over layout and behavior, use lightning-record-edit-form or lightning-record-view-form.

Build docs developers (and LLMs) love