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
aks001aks001 

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

Hi,

 

I'm having problem with this approach for creating invoiceLineItems, ultimately this is giving me an error saying that System.ListException: Before Insert or Upsert list must not have two identically equal elements..

now bellow is the code. if any one has any of the solutions that plz let me know.

          

 

       for(opportunityLineItem olis : opportunityLineItems)

                            Date WeekStartDate = olis.StartDate__c.date();
                            Date WeekEndDate = olis.EndDate__c.date();
                            Interval = startDate.daysBetween(endDate) / 7;
                          
                            if(interval == 0){
                               interval = 1;
                            }
                           
                           
                            for(integer i = 0; i < interval; i++){
                                InvoiceLineItem__c invoiceLineItem = new InvoiceLineItem__c();
                                if(interval > 1){
                                    invoiceLineItem.StartDate__c = WeekStartDate.addDays(sdays1);
                                    invoiceLineItem.EndDate__c = WeekStartDate.addDays(edays2);
                                    sdays1 = sdays1 + 7;
                                    edays2 = edays2 + 7;
                                }
                                else{
                                    invoiceLineItem.StartDate__c = WeekStartDate;
                                    invoiceLineItem.EndDate__c = WeekEndDate;
                                  
                                }
                                oliIli.add(new opportunityLineItemInvoiceLineItemWrapper(invoiceLineItem, olis));
                              
                            }

 

              }

 

 

 

Thanks in Advance...  :)

digamber.prasaddigamber.prasad

Hi,

 

Actually elements you are creating has all fieds identical, you need to have at least one field different for each element in list for successful DML operation on a list of records. I below code, I have added one line to differentiate records by different name. You can modify it to meet your exact requirement.

 

 for(opportunityLineItem olis : opportunityLineItems)
	Date WeekStartDate = olis.StartDate__c.date();
	Date WeekEndDate = olis.EndDate__c.date();
	Interval = startDate.daysBetween(endDate) / 7;
  
	if(interval == 0){
	   interval = 1;
	}
   
   
	for(integer i = 0; i < interval; i++){
		InvoiceLineItem__c invoiceLineItem = new InvoiceLineItem__c();
		if(interval > 1){
			invoiceLineItem.StartDate__c = WeekStartDate.addDays(sdays1);
			invoiceLineItem.EndDate__c = WeekStartDate.addDays(edays2);
			sdays1 = sdays1 + 7;
			edays2 = edays2 + 7;
		}
		else{
			invoiceLineItem.StartDate__c = WeekStartDate;
			invoiceLineItem.EndDate__c = WeekEndDate;
		  
		}
		
		invoiceLineItem.Name = 'Test' + i;
		oliIli.add(new opportunityLineItemInvoiceLineItemWrapper(invoiceLineItem, olis));
	  
	}

}

 Let me know if you have any issue with this.

 

Happy to help you!

 

 

 

 

digamber.prasaddigamber.prasad

If above doesn't help, could you please paste your complete code here, there are very highly likely chances that you are trying to add same element twice in a list some where else in your code.

 

Happy to help you!

SFDC_LearnerSFDC_Learner

Try with Upsert operation also.

aks001aks001

Hi digamberlucky,

 

Thanks for the consideration.....As you asked for rest of the code is bellow. I checked what you said and that doesn't works. i'm not getting why is this happening so plz help me out.... thanks... :)

 

for(opportunityLineItem olis : opportunityLineItems){

    Date WeekStartDate = olis.StartDate__c.date();
    Date WeekEndDate = olis.EndDate__c.date();
    Interval = startDate.daysBetween(endDate) / 7;

    if(interval == 0){
       interval = 1;
    }


    for(integer i = 0; i < interval; i++){
        InvoiceLineItem__c invoiceLineItem = new InvoiceLineItem__c();
        if(interval > 1){
            invoiceLineItem.StartDate__c = WeekStartDate.addDays(sdays1);
            invoiceLineItem.EndDate__c = WeekStartDate.addDays(edays2);
            sdays1 = sdays1 + 7;
            edays2 = edays2 + 7;
        }
        else{
            invoiceLineItem.StartDate__c = WeekStartDate;
            invoiceLineItem.EndDate__c = WeekEndDate;
          
        }
        oliIli.add(new opportunityLineItemInvoiceLineItemWrapper(invoiceLineItem, olis));
      
    }

}

 

for(opportunityLineItemInvoiceLineItemWrapper oliInvoiceLines : oliIli){
    for(opportunityInvoiceWrapper invoiceAfterInsert : invoiceAfterInsertP){
        if(invoiceAfterInsert.invoice.StartDate__c.month() == oliInvoiceLines.InvoiceLineItems.StartDate__c.month() && invoiceAfterInsert.invoice.StartDate__c.year() == oliInvoiceLines.InvoiceLineItems.StartDate__c.year()){
            oliInvoiceLines.InvoiceLineItems.Invoice__c = invoiceAfterInsert.invoice.id;
            system.debug('final line items.... ' + oliInvoiceLines.invoiceLineItems);
            invoiceLineItems.add(oliInvoiceLines.InvoiceLineItems);
        }
    }
}

upsert invoiceLineItems;

digamber.prasaddigamber.prasad

Hi,

 I assumet "invoiceLineItems" is list of InvoiceLineItem__c is a list, please make it set.

 

for(opportunityLineItem olis : opportunityLineItems){

    Date WeekStartDate = olis.StartDate__c.date();
    Date WeekEndDate = olis.EndDate__c.date();
    Interval = startDate.daysBetween(endDate) / 7;

    if(interval == 0){
       interval = 1;
    }


    for(integer i = 0; i < interval; i++){
        InvoiceLineItem__c invoiceLineItem = new InvoiceLineItem__c();
        if(interval > 1){
            invoiceLineItem.StartDate__c = WeekStartDate.addDays(sdays1);
            invoiceLineItem.EndDate__c = WeekStartDate.addDays(edays2);
            sdays1 = sdays1 + 7;
            edays2 = edays2 + 7;
        }
        else{
            invoiceLineItem.StartDate__c = WeekStartDate;
            invoiceLineItem.EndDate__c = WeekEndDate;
          
        }
        oliIli.add(new opportunityLineItemInvoiceLineItemWrapper(invoiceLineItem, olis));
      
    }

}

Set<InvoiceLineItem__c> invoiceLineItems = new Set<InvoiceLineItem__c>();
 
for(opportunityLineItemInvoiceLineItemWrapper oliInvoiceLines : oliIli){
    for(opportunityInvoiceWrapper invoiceAfterInsert : invoiceAfterInsertP){
        if(invoiceAfterInsert.invoice.StartDate__c.month() == oliInvoiceLines.InvoiceLineItems.StartDate__c.month() && invoiceAfterInsert.invoice.StartDate__c.year() == oliInvoiceLines.InvoiceLineItems.StartDate__c.year()){
            oliInvoiceLines.InvoiceLineItems.Invoice__c = invoiceAfterInsert.invoice.id;
            system.debug('final line items.... ' + oliInvoiceLines.invoiceLineItems);
            invoiceLineItems.add(oliInvoiceLines.InvoiceLineItems);
        }
    }
}

upsert invoiceLineItems;

 

Let me know if you have any issue.

 

 

aks001aks001

HI,

 

I really appreciate your answer . Yes, I tried so previously but it gives me an error that it should be a sObject or a list for DML.

digamber.prasaddigamber.prasad

Could you please replace upsert statement with below:-

 

invoiceLineItems
List<InvoiceLineItem__c> lstInvoice = new List<InvoiceLineItem__c>();
for(InvoiceLineItem__c inv : invoiceLineItems){
	lstInvoice.add(inv);
}

upsert lstInvoice;

 Let me know if you still see any issue.

 

aks001aks001

Hi Digambar,

 

I tried so what you said but still it's not working.

 

for(invoice__c inv : invoices){

    for(invoiceLineItem__c invl : invoiceLineItems){   // is a set
      if(inv.startDate__c.month() == invl.startDate__c.month() && inv.startDate__c.year() == invl.startDate__c.year()){
          invl.invoice__c = inv.id;
             iliToInsert.add(invl);  // is a list
       }
    }
}

upsert iliToInsert;

 

please let me know if you have any of the solutions thanks.... for consideration...      :)

digamber.prasaddigamber.prasad

Hi,

 

Could you please post your complete class or trigger here, so that I can see what exactly is going on.

CPRCPR
/ Error Line : FATAL_ERROR|System.ListException: Before Insert or Upsert list must not have two identically equal elements

@isTest

Public Class OpportunityPDFControllerTest{

    Static Opportunity opp;

    Static Billing_Event__c be;
    Static Account acc;
    Static Contact con;
  
    Static void init(){
        acc = new Account();
        acc.Name ='abc Account';
        insert acc;
        system.debug(acc);
      
        con = new Contact();
        con.LastName = 'abcd';
        insert con;
        system.debug(con);
      
        Location__c billingCountry = new Location__c();
        billingCountry.VAT_Percentage__c=5.0;
        billingCountry.Name ='DE';
      
        insert billingCountry;
        system.debug(billingCountry);
      
        opp= new Opportunity();
        opp.Receive_Bill__c = True;
        opp.Billing_Address__c = '123 abc address';
        opp.Account = acc;
        opp.Name='abcd';
        opp.StageName = 'Prospect' ;
        opp.CloseDate =System.today()+10;
        opp.Billing_Country__c = billingCountry.id;
      
        insert opp;
        system.debug(opp);
  
    }
  
   Static void createClickOut(){
          Click_Outs__c clickO = new Click_Outs__c();
        List<Click_Outs__c> clickOList = new List<Click_Outs__c>();
      
        clickO.CLICK_OUT_DATE__C = system.today().addDays(-3);
        clickO.RELATED_MERHCANT__C = opp.id;
        clickO.MERCHANT_OF_THE_DAY__C = true;
        clickO.MERCHANT_OF_THE_WEEK__C= false;
        clickO.COST_PER_CLICK__C= 0.33;
        clickOList.add(clickO);
        system.debug(clickO);
      
       clickO.CLICK_OUT_DATE__C = system.today().addDays(-2);
        clickO.RELATED_MERHCANT__C = opp.id;
        clickO.MERCHANT_OF_THE_DAY__C = false;
        clickO.MERCHANT_OF_THE_WEEK__C= true;
        clickO.COST_PER_CLICK__C= 0.30;
        clickOList.add(clickO);
        system.debug(clickO);
      
        clickO.CLICK_OUT_DATE__C = system.today().addDays(-1);
        clickO.RELATED_MERHCANT__C = opp.id;
        clickO.MERCHANT_OF_THE_DAY__C = false;
        clickO.MERCHANT_OF_THE_WEEK__C= false;
        clickO.COST_PER_CLICK__C= 0.34;
        clickOList.add(clickO);       
        system.debug(clickOList);   
      
        insert clickOList; // Error Line : FATAL_ERROR|System.ListException: Before Insert or Upsert list must not have two identically equal elements
        }

   /* Static void createClickOut(Decimal amount,Integer clickType){
         Decimal vatPer;
       
         clickO = new Click_Outs__c();
         vatPer = opp.VATPercentage__c;
         Decimal vat = (vatPer * clickO.COST_PER_CLICK__C)/100;
         clickO.Click_Out_Date__c = System.now()+2;
         clickO.Cost_Per_Click__c = amount;
       
         if(clickType==1)
             clickO.Merchant_Of_The_Day__c=True;
         else if(clickType==2)
             clickO.Merchant_Of_The_Week__c =True;
           
         clickO.Related_Merhcant__c = opp.id;
         insert clickO;
  
    }*/

  
    Public Static TestMethod void test1(){
        init(); 
        createClickOut();
                  
        Test.startTest();
            ApexPages.StandardController scon = new ApexPages.StandardController(opp);
            OpportunityPDFController pdfC = new OpportunityPDFController(scon);
         
            pdfC.billing.Billing_Cycle_Start_Date__c = system.today();
            pdfC.billing.Billing_Cycle_end_Date__c = system.today().addDays(8);
            pdfc.billing.refund__c=12;
          
            pdfc.refund=2;
          
            pdfC.refreshCheckout();
            pdfC.generatePDF();
          
            pdfC.generatePDFBill();
            pdfC.savePDF();
            ApexPages.currentPage().getParameters().get(pdfC.billing.id);
            pdfC.refreshCheckoutPDF();
          
        Test.stopTest();
  
    }
  
}
Muzammil BajariaMuzammil Bajaria
Thanx @digamber.prasad

your solution worked for me. List should have atleast one field different in each element.