Hi I am inserting two parents along with two child records each. I am getting the following error using the nested for loop.
System.ListException: Before Insert or Upsert list must not have two identically equal elements
Contructor:
public Quotelineitem_IBC(ApexPages.StandardController controller){
flag=true;
qouteLineItemMap=new map<Integer,Quote_line_Item__c>();
batchMap=new Map<integer,List<Batch__c>>();
//Capturing parent Quote id from URL
parentId=Apexpages.currentPage().getParameters().get('ParentId');
//Adding new QuoteLineItem without batches to map which will be displayed after loading the page
count=2;
count1=2;
List<Batch__c> listBatchToadd=new List<Batch__c>();
For(integer i=0;i<count;i++){
qouteLineItemMap.put(i+1,new Quote_line_Item__c(Quote1__c=parentId));
}
For(integer j=0;j<count1;j++){
batch__c b=new batch__c();
listBatchToadd.add(b);
batchMap.put(j+1,listBatchToadd);
}
quoteLineItemMapsize=qouteLineItemMap.size();
}//End of constructor
Save Method:
public pageReference save1(){
//Local varibles and instance
List<Quote_Line_item__c> listtoinsert =new List<Quote_Line_item__c>();
List<Batch__c> batchListToInsert =new List<Batch__c>();
List<Integer> sortkey=new List<Integer>();
sortkey.addAll(qouteLineItemMap.keySet());
for(integer qli:sortkey){
listtoinsert.add(qouteLineItemMap.get(qli));
}//End of for
// Inserting the list of QuoteLine Items
try{
upsert listtoinsert;
} catch(DmlException de){
return null;
}
//Iterating the loop to store the list of Batches which are going to be inserted into the databse
for(integer qli:sortkey){
for(Batch__c bat: batchMap.get(qli)){
if(bat.Asked_Qty__c!=Null){
bat.Quote_Line_Item__c=qouteLineItemMap.get(qli).id;
}
batchListToInsert.add(bat);
}//End of for
}// End of for
try{
upsert batchListToInsert;
} catch(DmlException de){
return null;
}
}//End of method
I have tried converting list to set and again set to list, but it still throws the same error.