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
d.tejdeep@nicomatic.ind.tejdeep@nicomatic.in 

Insert Mutiple parent along with Mutiple child in lightning

Parent Component:
 
<aura:iteration items="{!v.QuotelinitemList}" var="item" indexVar="index">
                <c:Child QliInstance="{!item}" rowIndex="{!index}" randNum="223"  aura:id="childcomp" qid="{!v.ParentId}" Language="{!v.Language}" oldqlino="{!v.oldqlino}"/>
            </aura:iteration>
Parent controller Js:
Save: function(component, event, helper) {
        var qid=component.get("v.ParentId");
        if (helper.validate(component, event)) {
            var action = component.get("c.SaveQlis");
            action.setParams({
                "QliList": component.get("v.QuotelinitemList"),
                "QuoteID" : qid
            });
            // set call back 
            action.setCallback(this, function(response){
                var state = response.getState();
                if (state === "SUCCESS") {
                    var qli=response.getReturnValue(); 
                        $A.createComponent(
                            "c:Child",{
                             "aura:id" : "childcomp"  
                            },function(newChild, status) {
                             if (component.isValid() && status === 'SUCCESS') {
                                    var key=0;
                                    for (key in qli){
                                    var qlisaved=qli[key].Id;
                                    var qliindexno=qli[key].QliRowNum__c;      
                                var childcomponent = component.find('childcomp');
                               childcomponent[key].mychildcompmethod(qliindexno,qlisaved); 
                                        key++;
                                }               
                          }    
                     });  
                }
            });
            $A.enqueueAction(action);
           component.set("v.Likedisable",true); 
        }

    },

Child component method :
 
<aura:attribute name="BatchList" type="Batch__c[]"/>
    <aura:method name="mychildcompmethod" action="{!c.saveBatchDetails}" access="public">
    <aura:attribute name="param1" type="integer"/>
    <aura:attribute name="param2" type="object"/>
    </aura:method>
            <aura:iteration items="{!v.BatchList}" var="item" indexVar="index">
<c:Grandchild  BatchInstance="{!item}" rowIndex="{!index}" qlinum="{!v.rowIndex}" />
            </aura:iteration>
Child Controller js :
 
saveBatchDetails: function(component,event,helper){
    console.log("calling updateRaces in ListRaceController");
    var qid=component.get("v.qid");
    var params = event.getParam('arguments');
    if (params) {
    var param1 = params.param1;
    var param2 = params.param2;
    }
    console.log(param1);
    console.log(param2);
            var par3=component.get("v.rowIndex")+1+component.get("v.oldqlino");
    console.log(par3);
   var action = component.get("c.SaveBatchDet");
    var par1=param1;
    var par2=component.get("v.BatchList");
        action.setParams({
            "Bt": component.get("v.BatchList"),
            "QliRowNum": par3,
            "Rowmap":{param1,par2},
            "QuoteID":qid
        });
        action.setCallback(this,function(resp){
        var state = resp.getState();
        if(state == "SUCCESS"){
            console.log( 'Add Batch Detail is saved');
            window.location.href = '/lightning/r/Quote__c/'+qid+'/view';
        }
        else if(state === "ERROR"){
            var errors = resp.getError();
            if (errors) {
                if (errors[0] && errors[0].message) {
                    console.log("Error message: " + 
                             errors[0].message);
                }
            } else {
                console.log("Unknown error");
            }


        }

    });

   $A.enqueueAction(action);   
}
Apex class :
 
@AuraEnabled
    public static list<Quote_Line_Item__c> SaveQlis(List<Quote_Line_Item__c> QliList,string QuoteID){

        Map<Integer, Quote_Line_Item__c> qouteLineItemMap = new Map<Integer, Quote_Line_Item__c>();
        integer i=1;
        for(Quote_Line_Item__c qli : QliList)
        {
          qouteLineItemMap.put(i, qli);
           i++; 
         }    

        transient List<Quote_Line_item__c> listtoinsert =new List<Quote_Line_item__c>();

        transient List<Integer> sortkey=new List<Integer>();
        sortkey.addAll(qouteLineItemMap.keySet());
        sortkey.sort();

        Integer rowCount=0;
        rowCount++;
        for(integer key : sortkey){
            if(qouteLineItemMap.containsKey(key) && qouteLineItemMap.get(key)!=null){
                qouteLineItemMap.get(key).QliRowNum__c = rowCount;
                qouteLineItemMap.get(key).Quote1__c=QuoteID;
                rowCount++;
            }
        }
        //Iterating the loop to store the list of QuoteLineItems which are going to be inserted into the databse
        for(integer qli:sortkey){
            if(qouteLineItemMap.get(qli).Name!='' && qouteLineItemMap.get(qli).name!=null){
            listtoinsert.add(qouteLineItemMap.get(qli));

            }
            // conditon to display the error when there is no batch for a particular QuoteLineItem
        }//End of for 

        // Inserting the list of QuoteLine Items
        try{
            system.debug(listtoinsert);
            upsert listtoinsert;
          //  system.debug(listtoinsert);
        } catch(DmlException de){
            return null;
        }
        System.debug('Number of Queries used in this apex code so far: ' + Limits.getQueries());
        return [SELECT QliRowNum__c,Id FROM Quote_Line_item__c where ID IN: listtoinsert];

    }



     @AuraEnabled
        public static list<batch__c> SaveBatchDet(integer QliRowNum,list<Batch__c> Bt,map<integer,list<batch__c>> Rowmap,string QuoteID){
            System.debug('beforedeleteNumber of Queries used in this apex code so far: ' + Limits.getQueries()); 

            //transient List<Integer> sortkey=new List<Integer>();
             Map<Integer,list<batch__c>> batchMap =  New Map<Integer,list<batch__c>>();
                batchMap.put(Integer.valueOf(QliRowNum),Bt);
             transient List<Batch__c> batchListToInsert =new List<Batch__c>();
             Map<integer,Quote_Line_Item__c> qouteLineItemMap = new Map<integer,Quote_Line_Item__c>();

            for(quote_line_item__c t: [SELECT QliRowNum__c, Id FROM quote_line_item__c WHERE quote1__c=:QuoteID])
            qouteLineItemMap.put(Integer.valueOf(t.QliRowNum__c), t);
            transient List<Integer> sortkey=new List<Integer>();
            sortkey.addAll(qouteLineItemMap.keySet());
          for(integer qli:sortkey){
              if(batchMap.get(qli)!=null)
                for(Batch__c bat: batchMap.get(qli)){
                    if(bat.Asked_Qty__c!=null && bat.Asked_Qty__c!=''){
                    bat.Quote_Line_Item__c=qouteLineItemMap.get(qli).id;
                    }
                    if(bat.Quote_Line_Item__c!=null)
                    batchListToInsert.add(bat);
                   // system.debug(batchListToInsert);
                }//End of for 
            }// End of for
            // Inserting the list of batches
            try{

              upsert batchListToInsert ;
                //system.debug(batchListToInsert);
            }catch(DmlException de){
                return null;
            }
            return null;
        }

I am getting 101 soql query error in "SaveBatchDet" apex class .
Form lightning parent component is calling mutiple times on child component action .
if i have 10 parents child is getting called 10 times .
Is there any way in "SaveBatchDet" apex to store all data and perform the action in a single insert .
Please try to help i working on this so many days