0

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.

2
  • try to convert List<Integer> sortkey=new List<Integer>(); this list into Set and check. Commented Aug 10, 2015 at 14:00
  • @TusharSharma still the same error
    – Teja
    Commented Aug 10, 2015 at 14:09

1 Answer 1

1

The for loop below is adding the same listBatchToadd list to your map on each iteration of the loop, which is why you have duplicate batch__c objects in your list.

    For(integer j=0;j<count1;j++){
        batch__c b=new batch__c();
        listBatchToadd.add(b);
        batchMap.put(j+1,listBatchToadd);
    }

To directly answer the question in the title of your post:

how to sort out this type of error

You are adding the same batch__c record multiple times to batchListToInsert in the line below. Figure out what is going on in your loops so that you only add the each instance to batch__c to the list once.

batchListToInsert.add(bat);
2
  • How can i proceed suggest me some way .I am sole employee in my company .Any idea would be very great full for me
    – Teja
    Commented Aug 10, 2015 at 14:18
  • 1
    Your code doesn't make sense to me. It seems to be a lot of random loops. Can you explain what you are trying to do and then someone should be able to help you simplify your code.
    – BarCotter
    Commented Aug 10, 2015 at 14:25

You must log in to answer this question.

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