lightning-record-edit-form : jayakrishnasfdc

lightning-record-edit-form
by: jayakrishnasfdc
blow post content copied from  Jayakrishna Ganjikunta
click here to view original post


Use the lightning-record-edit-form component to create a form that’s used to add a Salesforce record or update fields in an existing record on an object. The component displays fields with their labels and the current values, and enables you to edit their values.

lightning-record-edit-form supports the following features.

  • 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

If you don’t require customizations, use lightning-record-form instead.

To specify editable fields, use lightning-input-field components inside lightning-record-edit-form component. See the Editing a Record section.

To display record fields as read-only in lightning-record-edit-form, use lightning-output-field components to specify those fields. You can also use HTML and other display components such as lightning-formatted-name to display non-editable content.

To display all fields as read-only, use the lightning-record-form component with mode="readonly" or the lightning-record-view-form component instead of lightning-record-edit-form.

recordEditForm.html

<template>
        <lightning-card title="Account Details Record Edit Form ..">
            <lightning-layout>
                <lightning-layout-item>
                    <lightning-record-edit-form
                    object-api-name="Account"
                    onsuccess={handleSuccess}>
               <!-- We can use this simple version like designing of indivisual input field -->
                <lightning-input-field field-name={accountName}></lightning-input-field> 
               <lightning-input-field field-name={accountType}></lightning-input-field>

               <!-- Or we can use the other version like designing multiple input fields using for loop. -->
               <!--<template if:true={selectedFields}>
                   <template for:each={selectedFields} for:item="field">
                      <div key={field}>
                       <lightning-input-field field-name={field}></lightning-input-field>
                                </div>
                            </template>
                        </template>-->

           <lightning-button variant="brand" type="submit" name="save" label="Save"></lightning-button>
                    </lightning-record-edit-form>
                </lightning-layout-item>
            </lightning-layout>
        </lightning-card>
</template>

recordEditForm.js

import { LightningElement } from 'lwc';
 import { ShowToastEvent } from 'lightning/platformShowToastEvent';
 import ACCOUNT_NAME from '@salesforce/schema/Account.Name';
 import ACCOUNT_TYPE from '@salesforce/schema/Account.Type';
 export default class RecordEditForm extends LightningElement {
     accountName = ACCOUNT_NAME;
     accountType = ACCOUNT_TYPE;
     //selectedFields = [ACCOUNT_NAME, ACCOUNT_TYPE];
     handleSuccess(){
         if(this.recordId !== null){
             this.dispatchEvent(new ShowToastEvent({
                     title: "SUCCESS!",
                     message: "New record has been created.",
                    variant: "success",
                 }),  
            );    
          }
     } }

recordEditForm.js-meta.xml

<?xml version="1.0" encoding="UTF-8"?> <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata" fqn="recordEditForm">     <apiVersion>45.0</apiVersion>
     <isExposed>true</isExposed>
          <targets>
         <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
         <target>lightning__HomePage</target>
        </targets>
 </LightningComponentBundle>

output:

December 08, 2020 at 02:59PM
Click here for more details...

=============================
The original post is available in Jayakrishna Ganjikunta by jayakrishnasfdc
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Salesforce