+ Start a Discussion
JaanuJaanu 

New record is created instead of update in LWC?

I have to update Case record. So, I have created custom button, which call lightning component, which in turn calls lightning web component. LWC will display the screen perfectly. But when I enter the data and click save. New record is getting created instead of update. How to resolve this issue please ? I followed this link .. https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.reference_update_record . it did not work .. I have doubt.. the apex class ..it's quering one record from case.. we are not even passing any id .. how would it get the right case ????so, I modified the code ..still an issue.  Here is the code 

HTML FILE

<template>
 record-id={recordId} record-type-id={recordTypeId} onsuccess={handleSuccess}> -->
    <lightning-record-edit-form record-id={recordId} object-api-name="Case" columns="1"  mode="edit" density="auto" layout-type="Compact"  onsuccess={handleSuccess} onsubmit ={handleSubmit}>    
        <lightning-messages></lightning-messages>
              
        <!--cpoi-->
        <div class="slds-m-around_medium">
            <div class="slds-section">
                <h3 class="slds-text-heading_large slds-text-align_center">               
                    Testing Case Update          
                </h3>              
            </div>
            <br />
            <div class="slds-grid slds-gutters">
                <div class="slds-col slds-size_1-of-2 csclass">
                    <lightning-input-field field-name='Test_Date__c'></lightning-input-field>
                </div>
            </div>
        <lightning-button variant="brand-outline" type="submit" name="save" label="Update Case" onclick={showHandler} class="slds-align_absolute-center"></lightning-button>
        </div>
    </lightning-record-edit-form>
</template>

Java Script

import { LightningElement, api } from 'lwc';

export default class Test_Case extends LightningElement {
    @api recordId;
    handleSubmit(event) {
     console.log('onsubmit: '+ event.detail.fields);
 
    }
    handleSuccess(event) {
        const updatedRecord = event.detail.id;
       
        console.log('onsuccess: ', updatedRecord);
    }
    
XML File    
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>47.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordPage</target>
        <target>lightningCommunity__Page</target>
        <target>lightningCommunity__Default</target>
    </targets>
        <targetConfigs>
        <targetConfig targets="lightningCommunity__Default">
            <property
                name="recordId"
                type="String"
                label="Record Id"
                description="Automatically bind the page's record id to the component variable"
                default="{!recordId}" />
        </targetConfig>
    </targetConfigs>
</LightningComponentBundle>