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 

list must not have two identically equal elements

List<Batch__c> listBatchToadd=new List<Batch__c>();
          For(integer j=0;j<2;j++){
            batch__c b=new batch__c();
            listBatchToadd.add(b);
         }

        For(integer i=0;i<count;i++){
               qouteLineItemMap.put(i+1,new Quote_line_Item__c(Quote1__c=parentId));
               //  batchMap.put(i+1,new list<batch__c>();
                  batchMap.put(i+1,new list<batch__c>(listBatchToadd));
                }
System.ListException: Before Insert or Upsert list must not have two identically equal elements

I am getting this error here 
new list<batch__c>(listBatchToadd)

How to get rid of this error ?
 
Neetu_BansalNeetu_Bansal
Hi,

You are adding two identical records in the list. As I understand, all Batch records are empty so these are considered as Identical records.

Please let me know your excat reuirment only than I'll be able to help you.

Thanks,
Neetu
d.tejdeep@nicomatic.ind.tejdeep@nicomatic.in
Hi 

I am preapring layout for insert two parents attached with two childs each 

Consider Quote_line_Item__c is parent ,Batch_c is child

Please see the figure then you will get this :User-added image


 
Neetu_BansalNeetu_Bansal
Hi,

You must be adding same data in the batch, you cannot insert or upsert identical data in salesforce.

Try using different values in Batch and you will be able to do that.

Thanks,
Neetu
d.tejdeep@nicomatic.ind.tejdeep@nicomatic.in
Yes i will be adding some values inside that .If i added soem values also .It is showing the same error


 
d.tejdeep@nicomatic.ind.tejdeep@nicomatic.in
Hi i have kept like this 

Batch__c b= new batch__c(master_id__c='teja'+j);

Even when i insert the values in the vf still i am facing the same error
d.tejdeep@nicomatic.ind.tejdeep@nicomatic.in
Hi ,

Visualforce page :
 
<apex:page id="mainPage" standardController="Quote_Line_Item__c"  extensions="Quotelineitem_IBC" sidebar="false"  standardStylesheets="false" >
    <head>
        <apex:includeScript value="{!URLFOR($Resource.JqueryA, 'js/jquery-1.6.2.min.js')}"/>
        <apex:includeScript value="{!URLFOR($Resource.JqueryA, 'js/jquery-ui-1.8.16.custom.min.js')}"/>
        <apex:stylesheet value="{!URLFOR($Resource.JqueryA, 'css/ui-lightness/jquery-ui-1.8.16.custom.css')}"/>
    </head>
    <apex:form id="myForm">
        <apex:actionFunction name="errorMessage"  action="{!save1}"  />
        <apex:pageblock id="pb">
            <center id="cntr"> 
               <apex:variable value="{!1}" var="rowNum"/> 
               <apex:repeat value="{!qouteLineItemMap}" var="qliRow" id="theQliRepeat">
                    <apex:repeat value="{!qouteLineItemMap[qliRow]}" var="qli" id="therepeat1">
                  
                        <table border="1" width="90%" style="border-collapse:collapse;" bordercolor = "#000000" id="tble{!rowNum}" class="QliTble">
                            <tr>
                                <td>        
                                    <table border="0" cellpadding="0" cellspacing="0" width="100%" id="tbleqli{!rowNum}">
                                        <br/>
                                        <tr>            
                                            <td style="padding-right:4px;padding-left:6px">&nbsp;</td>
                                            <td></td>  
                                            <td><b> P/N</b></td>        
                                            <td ><b>Cl P/N </b></td>
                                            <td><b>Description</b></td>
                                            <td><b>Lead Time</b></td>          
                                            <td><b>Other Lead Time</b></td>         
                                            <td><b>Comments</b></td>
                                            <td><b>Comments 2/Line Item Validity</b></td>
                                                
                                            <td ></td>             
                                        </tr>        
                                        <tr>           
                                             <apex:variable value="{!1}" var="batchrowNum" id="vrbatch"/>
                                            <td id="Nico{!(ROUND(rowNum,0))}">
                                                <apex:inputfield value="{!qli.Name}" id="Name" style="width:110px"   StyleClass="AutoPin" onmouseover="javascript: nicopinClient  = '{!(ROUND(rowNum,0))}'; " />      
                                            </td>      
                                            <td id="client{!(ROUND(rowNum,0))}">
                                                <apex:inputField value="{!qli.Client_P_N__c}" style="width:110px;"  /> 
                                            </td>
                                            <td id="desc{!(ROUND(rowNum,0))}">
                                                <apex:inputfield value="{!qli.Description__c}" style="width:140px;height:65px;ength:10px;"/>
                                            </td>
                                            <td>
                                                <apex:inputfield value="{!qli.Standard_Estimated_Lead_Time__c}"  style="width:100px"/>
                                            </td>        
                                            <td>
                                                <apex:inputfield value="{!qli.Other_Lead_Time__c}" style="width:100px"/>
                                            </td>
                                            <td>
                                                <apex:inputfield value="{!qli.Comments__c}" style="width:140px;height:65px;ength:10px;"/>
                                            </td> 
                                            <td>
                                                <apex:inputfield value="{!qli.Quote_Validity__c}"   id="validity" style="width:140px;height:65px;ength:10px;" styleClass="input-class"/>
                                                
                                            </td>
                                        </tr>
                               
                                    </table>      
                                    <br/> 
                                   
                                   <apex:variable value="{!1}" var="incrementbatchsize" id="vrbatchs" />
                                    <apex:repeat value="{!batchMap}" var="bat" id="rpbatch">
                                    <apex:outputPanel rendered="{!bat==qliRow}">
                                        <apex:repeat value="{!batchMap[qliRow]}" var="child" id="therepeat2">
                                           
                                            <table border="0" cellpadding="0" id="inTble{!incrementbatchsize}" cellspacing="0" width="100%">
                                                <tr>
                                                    <td style="padding-left:210px;"></td>                 
                                                    <td><b>Asked Quantity</b></td>
                                                    <td><b>Batch Quantity</b></td> 
                                                    <td><b>Max Quantity</b></td>
                                                                                          
                                                    <td><b>Unit Price</b></td>
                                                    <td><b>Confirm Batch</b></td>
                                                    <td style="padding-right:410px;"></td>
                                                </tr>
                                                <tr>
                                                    <td style="padding-left:210px;"></td>                   
                                                    <td><apex:inputfield value="{!child.Asked_Qty__c}" required="true" style="width:110px" id="askqty" styleClass="{!incrementbatchsize}"/></td>
                                                    <td> <apex:inputfield value="{!child.Quantity__c}"  required="true" style="width:110px"/></td> 
                                                    <td> <apex:inputfield value="{!child.Max_Quantity__c}"  style="width:110px"/></td>
                                                    <td><apex:inputfield value="{!child.Unit_Price__c}"  required="true"  style="width:110px"/></td>
                                                    <td><apex:inputfield value="{!child.confirm_Order__c}"  style="width:20px"/></td>
                                                    <td style="padding-right:410px;"></td>        
                                                </tr>
                                                <apex:variable var="batchrowNum" value="{!batchrowNum+ 1}"/>  
                                                 <apex:variable value="{!incrementbatchsize+1}" var="incrementbatchsize"/>
                                        </table>
                                        
                                    </apex:repeat>
                                      
                                    </apex:outputPanel>
                                    </apex:repeat>
                                       
                                </td>
                            </tr>
                        </table> 
                        <apex:variable var="rowNum" value="{!rowNum + 1}"/>                 
                </apex:repeat>
            </apex:repeat>
            <apex:pageBlockButtons > 



               <apex:commandbutton value="save" action="{!save1}"  />  

                
            </apex:pageBlockButtons> 
        </center> 
        </apex:pageBlock>
   </apex:form>   
</apex:page>
Controller :

 
public  class Quotelineitem_IBC{
    //varibles
    Public Boolean flag{get;set;}

    Public Id parentId{get;set;}
    public integer Rowcount{get; set;}
    public Integer qliRowNum1{get;set;}
    public Integer qliRowNum{get;set;}
    public Integer finalRowNum{get;set;}
    Public Integer deleteBatchRow{get;set;}
    Public Integer deleteQliRow{get;set;}
    public Integer quoteLineItemMapsize{get;set;}
        integer count;
    integer count1;
    //map variables
    Public Map<Integer,Quote_line_Item__c> qouteLineItemMap{get;set;}
    public Map<integer,List<Batch__c>> batchMap{get;set;}

    //Constructor declaration
   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;
        
        List<Batch__c> listBatchToadd=new List<Batch__c>();
          For(integer j=0;j<2;j++){
            batch__c b=new batch__c(master_id__c='teja');
            listBatchToadd.add(b);
         }

        For(integer i=0;i<count;i++){
               qouteLineItemMap.put(i+1,new Quote_line_Item__c(Quote1__c=parentId));
               //  batchMap.put(i+1,listBatchToadd);
                  batchMap.put(i+1,new list<batch__c>(listBatchToadd));
                }
         //batchMap.put(qliRowNum,listBatchToadd);
        quoteLineItemMapsize=qouteLineItemMap.size();
    }//End of constructor
    //Method to add quotelineitem under the particular quotelineitem 

    public pageReference save1(){

        
        
        flag=true;
        //Local varibles and instance 
        List<Quote_Line_item__c> listtoinsert =new List<Quote_Line_item__c>();
        List<Batch__c> batchListToInsert =new List<Batch__c>();
        set<Integer> sortkey=new set<Integer>();
       
        //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!=NULL){
            listtoinsert.add(qouteLineItemMap.get(qli));
             }
             
             System.debug('^^^^^^^^^^^listtoinsert^^^^^^^^^'+listtoinsert);
          
        }//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

        // Inserting the list of batches
        try{
           upsert batchListToInsert;
        } catch(DmlException de){ 
             Delete listtoinsert;
             return null;
        }

        // After inserting the QuoteLineItems and batches returign to the parent Quote
       PageReference pr = new PageReference('/'+parentId);  
                pr.setRedirect(True);

                return pr;
             //return null;
    }//End of method

}//End of main class