function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
anantha Lakshmi kuppalaanantha Lakshmi kuppala 

existing record not updating, it is inserting new record. I would like to update new record. Any suggestions please?

import { api, LightningElement } from 'lwc';
import contactFirstName from '@salesforce/schema/Contact.FirstName';
import contactLastName from '@salesforce/schema/Contact.LastName';
import contactEmail from '@salesforce/schema/Contact.Email';
import contactPhone from '@salesforce/schema/Contact.Phone';
import contactAccount from '@salesforce/schema/Contact.AccountId';
import {ShowToastEvent} from 'lightning/platformShowToastEvent';
 
export default class updateRecordLwc extends LightningElement {
    @api ContactObjectApiName='Contact';
    @api recordId;
    ContactFieldList = [contactFirstName,contactLastName,contactEmail,contactPhone,contactAccount];
 
    contactHandleUpdate(event){
        const evt = new ShowToastEvent({
            title:'Record Updated',
            message:'Contact record: ' + event.detail.fields.LastName.value + 'is successfully updated',
            variant:'success',
          })
          this.dispatchEvent(evt);
    }
}

Html
<template>
    <lightning-card title="LWC Example 2">
        <br/><br/>
        <h2 class="slds-text-color--error">How to Edit/Update record without apex class by help of the lightning data service in LWC</h2>
 
        <lightning-record-form object-api-name={ContactObjectApiName} fields={ContactFieldList} record-id={recordId} onsuccess={contactHandleUpdate}></lightning-record-form>
 
<br/><br/>
</lightning-card>
</template>
mukesh guptamukesh gupta
Hi Anantha,

Please follow below code , you need to use lightning-record-edit-form
<template>
    <lightning-record-edit-form record-id={recordId} object-api-name="Contact"
            onsuccess={handleSuccess} onsubmit ={handleSubmit}>
        <lightning-messages>
        </lightning-messages>
        <lightning-output-field field-name="AccountId">
        </lightning-output-field>
        <lightning-input-field field-name="FirstName">
        </lightning-input-field>
        <lightning-input-field field-name="LastName">
        </lightning-input-field>
        <lightning-input-field field-name="Email">
        </lightning-input-field>
        <lightning-button class="slds-m-top_small" variant="brand" type="submit" name="update" label="Update">
        </lightning-button>
    </lightning-record-edit-form>
</template>
 
import { LightningElement, api} from 'lwc';
export default class RecordEditFormEditExampleLWC extends LightningElement {
    @api recordId;
    handleSubmit(event) {
        console.log('onsubmit event recordEditForm'+ event.detail.fields);
    }
    handleSuccess(event) {
        console.log('onsuccess event recordEditForm', event.detail.id);
    }
}

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh 

 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Lakshmi,

Refer the below link have similar kind of solution.

https://www.swdcworld.com/2020/08/update-any-record-without-using-apex.html

Thanks!!