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 

how to get rid of this error ?System.ListException: Before Insert or Upsert list must not have two identically equal elements

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

I am trying to insert two parents with two childs attached each .

This is my 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 am getting the error like this

System.ListException: Before Insert or Upsert list must not have two identically equal elements 

It is throwing the error in this 
upsert batchListToInsert;

Please provide me best solution for this :

Thanks in Advance

 
William TranWilliam Tran
Try using set instead of list, instead of rewriting, I created a set based on your list, hopefully this will do it:

batchSetToInsert= new Set<Batch__c>(batchListToInsert);
upsert batchSetToInsert;

Here's the full code:
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>();
        Set<Batch__c> batchSetToInsert;
        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{


         batchSetToInsert= new Set<Batch__c>(batchListToInsert);

            upsert batchSetToInsert;
        } catch(DmlException de){ 
             return null;
        }
    }//End of method

 
d.tejdeep@nicomatic.ind.tejdeep@nicomatic.in
Hi 

Thanks for your reply 

But still i am facing the issue

 
William TranWilliam Tran
Try this then:

chagne this;
batchSetToInsert= new Set<Batch__c>(batchListToInsert);
upsert batchSetToInsert;

To this:
batchSetToInsert= new Set<Batch__c>();
batchSetToInsert.addAll(batchListToInsert);
batchListToInsert.addAll(batchSetToInsert);
upsert batchListToInsert;

If this does not work then I will need to test myself.

thx