1

On my lighting-record-edit form, I want to set the record-id to that of a newly saved opportunity. With the help of this site, I now can retrieve the id of the saved record. However, I have an issue setting the variable used for the forms record-id. My code is below. I can see that event.detail.id is returning the new opportunity id, however, when I try to set 'this.selectedOppId = event.detail.id;' I get an error, 'Cannot Read property value of undefined' I'm really confused. The record does get created and the ID appears to update but I get the error. I appreciate any help. Thanks!!!!

Here is error that displays in the console: here is error {"message":"Cannot read property 'value' of undefined","detail":"","output":{}}

//form:

<lightning-record-edit-form record-id={selectedOppId} 
        object-api-name="Opportunity" record-type-id={recordTypeId}
        onsuccess={onSuccess} onsubmit ={onSubmit} onerror={handleError}>

//Javascript

@track selectedOppId;
onSuccess(event){
    this.loading = false ;
    this.oppid = event.detail.id;
        console.log(' !!!! ' + event.detail.id);
    this.accid = event.detail.fields.AccountId.value; 
 
     //display tabs
     this.showrelated = true;
        //this line throws a 'Cannot Read property value of undefined'
     this.selectedOppId = event.detail.id;
        console.log('selectedOppId ' + this.selectedOppId);
     
    this.dispatchEvent(new ShowToastEvent({
        title: 'Opportunity Updated',
        message:  'You have updated the Opportunity',
        variant: 'success'
    })); 
}
6
  • Can you try rendering form conditionally like <template if:true={selectedOppId}><<lightning-record-edit-form...></template> ?
    – dzh
    Oct 6, 2020 at 3:12
  • Also you might need to check if event and detail is not null if(event && event.detail && event.detail.id) { //assign this.selectedOppId and show toast }
    – dzh
    Oct 6, 2020 at 3:14
  • Thanks for your help! I wrapped the entire method in the if statement but still getting the error. I added the error I get in console to the question. thanks!!
    – Frank
    Oct 6, 2020 at 12:50
  • Try Chromes “pause on exception” to trace where it fails exactly. Note you’ve got a space after onsubmit and do you have recordtype value?
    – dzh
    Oct 6, 2020 at 20:28
  • 1
    dzh, that was perfect, I now know that cause. The error is on a lookup to user field. I must be missing something needed for a lookup field. The error is hard for me to translate but among the text is object.computeRecordPills Thanks!!!
    – Frank
    Oct 7, 2020 at 2:28

1 Answer 1

0

I am able to set the record-id after record creation in lightning-record-edit-form.

enter image description here

html

<template>
    <lightning-record-edit-form record-id={selectedOppId} onsuccess={onSuccess} object-api-name="Contact">
        <lightning-messages>
        </lightning-messages>
        <lightning-input-field field-name="AccountId">
        </lightning-input-field>
        <lightning-output-field field-name="Id">
        </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>

JS

import { LightningElement, track } from 'lwc';

export default class ParentLightningWebComponent extends LightningElement {
    @track selectedOppId;
    accid = '';
    onSuccess(event){
        //this line throws a 'Cannot Read property value of undefined'
        this.selectedOppId = event.detail.id;
        console.log('selectedOppId ' + this.selectedOppId);

        this.accid = event.detail.fields.AccountId.value; 
    }
}
1
  • Thanks Sanket. I can update the id also but I am getting the error, 'Cannot Read property value of undefined' Just can't figure out why the error. thanks!
    – Frank
    Oct 6, 2020 at 17:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .