0

I am trying to pass the listofWrapper-> (List) data to Apex method for DML operation. I am not able to figure out how to get Outer loop index ("Oppindex") for any change in the child iteration fields while saving. Or any other Solution for saving the Wrapper Data.

My Wrapper class looks like below:-

public class OppConWrapper {
        @AuraEnabled 
        public Integer index {get;set;}
        @AuraEnabled 
        public Opp__c OppRec {get;set;}
        @AuraEnabled
        public list<Con__c> listCon {get;set;}

        public OppConWrapper(.....){.....}
}

HTML file:-

<lightning-button label="Save &amp; Close" title="Save" onclick={handleSaveClose} class="slds-m-left_x-small"></lightning-button>

    <template if:true={listOppWrapper}>
        <template for:each={listOppWrapper} for:item="OppWrap" for:index="Oppindex">
           <lightning-record-edit-form record-id={OppWrap.oppRec.id} object-api-name="Opp__c">
            <lightning-input-field class='inputOpp'...... ></lightning-input-field>
            </<lightning-record-edit>
            <template for:each={OppWrap.listCon} for:item="con" for:index="conIndex">
              <lightning-record-edit-form record-id={con.id} object-api-name="Con__c">
              <lightning-input-field class='inputCon'...... ></lightning-input-field>
              </<lightning-record-edit>
            </template>
        </template>
    </template>

JS File:-

handleSaveClose(event){
  let inputConFields = this.template.querySelectorAll('.inputCon');
  let inputOppFields = this.template.querySelectorAll('.inputOpp');
}
5
  • So are you trying to get the index of the opportunity for the particular con__c? Sep 22, 2020 at 17:44
  • Yes, so that i can pass the list of con as per Opp__c index and perform the DML. Sep 22, 2020 at 17:58
  • Will addEventListner help? if yes then how can i use with this.template.querySelectorAll('.inputCon'). ? Sep 22, 2020 at 17:59
  • I figured out the solution. But if other than index mapping. Is there any other way to update wrapper data then please let me know, Sep 22, 2020 at 18:50
  • find them based on the ids Sep 22, 2020 at 19:30

1 Answer 1

1

Change your HTML design to the parent-child relationship

parent component

 <template if:true={listOppWrapper}>
    <template for:each={listOppWrapper} for:item="OppWrap" for:index="Oppindex">
       <lightning-record-edit-form record-id={OppWrap.oppRec.id} object-api-name="Opp__c">
        <lightning-input-field class='inputOpp'...... ></lightning-input-field>
        </lightning-record-edit>
           <template for:each={OppWrap.listCon} for:item="con" for:index="conIndex">
              <c-child record-id={con.id} api-name="Con__c"></c-dhild>
         </template>
    </template>
</template>

in JS

handleSaveClose(event){
  let inputConFields = this.template.querySelectorAll('c-child');
  Array.from(inputConFields).forEach(item=>{
    console.log(item.fetchInputData())
  })
  let inputOppFields = this.template.querySelectorAll('.inputOpp');
}

child component

@api recordId
@api apiName
@api fetchInputData(){
  return this.template.querySelectorAll('.inputCon').value;
}

In child html

<template>
<lightning-record-edit-form record-id={recordId} object-api-name={apiName}>
      <lightning-input-field class={inputCon}...... ></lightning-input-field>
 </lightning-record-edit>
</template>
2
  • Thanku @nikhil , i followed your approach, now i am able to figure out the child input fields with parent index. I have added few logic in '@api fetchInputData()' but this api method is returning undefined value to parent. api property can be shared to parent by just passing the value in child component <c-child record-id={con.id} api-name="Con__c" updated-inputs={property}> -- but it's not working for me. I am trying with custom Event approach now. Sep 23, 2020 at 12:10
  • with the Custom Event, It's working, Thanku so much for help @nikhilKarkra. I was really struggling to fix the issue. Sep 23, 2020 at 12:51

You must log in to answer this question.

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