0

I am beginner to LWC,I have few queries

My queries are:

In Aura components we are using LDS for single records, In LWC also we have similar concepts

 <lightning-record-form record-id={recordId} object-api-name={objectApiName} fields={fields} columns="2" mode="readonly" onsubmit={handleSubmit}></lightning-record-form>

Q1. In mode we can change to Edit,view directly, then why do we need lightning-record-edit-form and lightning-record-view-form

Q2. In Lightning lightning-record-form,lightning-record-edit-form,lightning-record-view-form also we will able to create records then Why do we need UI API for creating Records

Q3 When we have to go for UI API

Please explain above concepts with some business scenarios

1

Q1.

In mode we can change to Edit, view directly, then why do we need lightning-record-edit-form and lightning-record-view-form?

Answer

The lightning-record-form renders the form based on the standard layouts configured. Here are the reasons we use it.

  • Switches between view and edit modes automatically when the user begins editing a field in a view form
  • Provides Cancel and Save buttons automatically in edit forms
  • Uses the object's default record layout with support for multiple columns
  • Loads all fields in the object's compact or full layout, or only the fields you specify

So basically, we can not controlled which fields are displayed. The fields displayed based on the layout type and layout defined.

The reason we use lightning-record-view-form or lightning-record-edit-form instead of form is that you can explicitely render the fields, let's say you want dynamically show/hide the field based on anothe field value. Also,

  • Editing a record's specified fields, given the record ID.
  • Creating a record using specified fields.
  • Customizing the form layout
  • Custom rendering of record data

Q2 & Q3

In Lightning lightning-record-form, lightning-record-edit-form,lightning-record-view-form also we will able to create records then Why do we need UI API for creating Records

When we have to go for UI API

Answer

We use UI Record API when we need to programmatic access to the record but the UI form is not needed. Like the record input is hidden from user.

Example: Use geRecord to get the record which you don't want to show on page, but it is needed for some calculation or to set components' visibility.

How to select between these options?

Thumb rule is to go for easy solution which is record-form, then check if is suitable for your requirement, if not then you can choose other options. Specifically you can decide by answering these questions?

  • Do we need to utilize the sandard layout or customized layout? So whatever fields on the layout will be automcatically available on the form.
  • Do we need a custom styling to display a record?
  • Do we need to control the visibility of the fields based on the field values on record?
  • Do we need to show the record to the user?
1

Q1. In mode we can change to Edit,view directly, then why do we need lightning-record-edit-form and lightning-record-view-form

  • As per the documentation:

    Use the lightning-record-form component to quickly create forms to add, view, or update a record.

  • The functionality of the record view form is very limited, here you don't have ability for any customization than what is being offered by the base component.

      <lightning-record-form
          record-id="001XXXXXXXXXXXXXXX"
          object-api-name="Account"
          layout-type="Full"
          mode="view">
      </lightning-record-form>
    

Q2. In Lightning lightning-record-form,lightning-record-edit-form,lightning-record-view-form also we will able to create records then Why do we need UI API for creating Records

  • The lightning-record-view/edit-form uses UI API under the hood, this offers more customization like you can specify your own set of fields; that would be used for creating or editing of the records.
  • This also doesn't need a lot of code, use this for adding any custom validations or any business logic.

Q3 When we have to go for UI API

  • lightning/uiRecordApi should can be used when the first two options doesn't work.
  • These are some wire JS methods that can be used to create/update/even delete records, etc. By building the custom UI, you can use these wire methods for doing your own logic.
  • This would be quite some work so only use this if you need to do something fully custom.

Last resort to look for would be going by apex route and write custom logic, but think from the maintenance perspective and see which options works best for you.

Not the answer you're looking for? Browse other questions tagged or ask your own question.