Usage Considerations for Working with Records

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.

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.

To retrieve record type information, use the getObjectInfo wire adapter.

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.

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.

The recordTypeInfos property returns a map of record type Ids that are available in your org.

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.

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.