Working with records using base components make Lightning Data Service available to you without additional configuration, but there are a few custom use cases to consider.
Consider the following use cases.
To display a record form based on a record type, which includes picklist values based on the record type, get the record type Id.
To notify a parent component about a successful record submission, dispatch and handle a custom event.
Get Record Type Id
Picklist fields display values according to your record types. When working with lightning-record-form or lightning-record-edit-form, you must provide a record type Id if you have multiple record types on an object and you don’t have a default record type. Otherwise, the default record type Id is used.
Display a record create form based on a record type by providing the record-type-id attribute. This example shows a form that you can place on an account record page. The form displays fields, which include a picklist with values based on the given record type Id.
1<template>2 <lightning-card title="Record Form with Record Type" icon-name="standard:account">3 <div lwc:if={objectInfo.data} class="slds-m-around_medium">4 <lightning-record-form5 object-api-name={objectApiName}6 record-type-id={recordTypeId}7 fields={fields}8 >9 </lightning-record-form>10 </div>11 </lightning-card>12</template>
Import the getObjectInfo module and a reference to the account object. The recordTypeId getter returns the Id that matches the record type name Special Account.
1import{LightningElement, api, wire, track}from "lwc";2import{getObjectInfo}from "lightning/uiObjectInfoApi";3import ACCOUNT_OBJECT from "@salesforce/schema/Account";4import NAME_FIELD from "@salesforce/schema/Account.Name";5import PHONE_FIELD from "@salesforce/schema/Account.Phone";6import INDUSTRY_FIELD from "@salesforce/schema/Account.Industry";78export default class RecordFormWithRecordType extends LightningElement{9 // Flexipage provides recordId and objectApiName10 @api recordId;11 @api objectApiName;1213 @track objectInfo;1415 // Define fields to display in form16 // Industry field is a picklist17 fields = [NAME_FIELD, PHONE_FIELD, INDUSTRY_FIELD];1819 @wire(getObjectInfo, {objectApiName: ACCOUNT_OBJECT})20 objectInfo;2122 get recordTypeId(){23 const rtis = this.objectInfo.data.recordTypeInfos;24 return Object.keys(rtis).find((rti)=> rtis[rti].name === "Special Account");25}26}
The recordTypeInfos property returns a map of record type Ids that are available in your org.
Handle a Custom Event on a Form
The lightning-record-form, lightning-record-edit-form, and lightning-record-view-form components come with event handling via onsubmit, onsuccess, and onerror attributes. To pass your form data to the container component, create a custom event.
Let’s say you have a container component c-wrapper, and your form is in the component c-account-creator. Let’s pass the Id of the new record to the container component. In c-wrapper, create an instance of c-account-creator.
When the record is created successfully, the newrecord event is dispatched by the onsuccess handler on lightning-record-form, which then calls the handleNewRecord method with the record Id.
1// wrapper.js2import{LightningElement}from "lwc";34export default class Wrapper extends LightningElement{5 recordId;67 /**8 * Handles the new record event.9 */10 handleNewRecord(evt){11 const recordId = evt.detail.data.id;12 this.recordId = recordId;13}14}
In c-account-creator, the createStatus property displays a message with the Id of the newly created record.
The Id is not available on the submit event. Use the success event to return the Id.
The handleAccountCreated method handles the success event.
1import{LightningElement}from "lwc";2import ACCOUNT_OBJECT from "@salesforce/schema/Account";3import NAME_FIELD from "@salesforce/schema/Account.Name";4import WEBSITE_FIELD from "@salesforce/schema/Account.Website";56/**7 * Creates Account records.8 */9export default class AccountCreator extends LightningElement{10 /** Status message when creating an Account. */11 createStatus = "";1213 accountObject = ACCOUNT_OBJECT;1415 accountFields = [NAME_FIELD, WEBSITE_FIELD];1617 /** Handles successful Account creation. */18 handleAccountCreated(evt){19 this.createStatus = `Account record created. Id is ${evt.detail.id}.`;2021 const event = new CustomEvent("newrecord", {22 detail:{data: evt.detail},23});24 this.dispatchEvent(event);25}26}
Override Standard Actions
To replace standard actions like New, Edit, View, Clone, or List buttons on object pages (similar to standard action overrides in Visualforce), wrap your Lightning web component in an Aura component that implements the lightning:actionOverride interface.
To learn about which experiences support Lightning web components directly, see Supported Experiences.
Quick Actions are different from action overrides. Quick Actions add custom buttons to page layouts, while action overrides replace standard platform buttons. To add custom actions with Lightning web components, see Use Quick Actions.