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
ministe_2003ministe_2003 

Before Insert or Upsert list must not have two identically equal elements

Hi all,
I'm trying to insert two new opportunities into the system in Apex based on user input.  The values will be identical on both, apart from the Close Date which will always be different.  However on the insert call, I'm getting the error in the subject.  Does anyone know why I'm getting this and how to get around it?  Why can I not insert more than one opportunity at once, regardless of the fact many of the fields are identical?  Here's the code I'm using to create the opportunity records, note that popOpp is an opportunity record created in Apex, and it's fields are populated in a visualforce page.  I then apply these values to new opportunityrecords and denamically populate the close date:

Date closeDate = fw.Start_Date__c;
for(integer i = 1; i <= numOpps; i++){
    opportunity opp = new Opportunity(
     Name = fw.Name+' - '+monthMap.get(closeDate.Month())+' '+closeDate.Year(),
     CloseDate = CloseDate,
     AccountId = popOpp.AccountId,
     StageName = 'R3 - Qualified Lead',
     Booking_Entity__c = popOpp.Booking_Entity__c,
     Order_to_place__c = popOpp.Order_to_place__c,
     Booking_Site__c = popOpp.Booking_Site__c,
     Budget_quote__c = popOpp.Budget_quote__c,
     Industry__c = popOpp.Industry__c,
     Received_Date__c = popOpp.Received_Date__c,
     End_User_Company_Name__c = popOpp.End_User_Company_Name__c,
     Quote_Required_By_Date__c = popOpp.Quote_Required_By_Date__c,
     Country__c = popOpp.Country__c,
     Date_Quoted__c = popOpp.Date_Quoted__c,
     Terms_Conditions__c = popOpp.Terms_Conditions__c,
     Quoted_Lead_Time__c = popOpp.Quoted_Lead_Time__c,
     CurrencyIsoCode = popOpp.CurrencyIsoCode,
     OwnerId = popOpp.OwnerId
    );
    oppList.add(opp);
    closeDate = closeDate.addMonths(selectedFreq);
   }
  }
...
Database.SaveResult[] oppSrList;
   try{
    oppSrList = Database.insert(oppList);   <--ERROR IS HERE
   }catch(Exception e){
    ApexPages.addMessages(e);
    return;
   }

Thanks
Deepak Kumar ShyoranDeepak Kumar Shyoran
To avoid this error First add elements in Set which you want insert and then in last where you are going to perform DML copy data from that set to a list because DML can't be performed on Set.
ministe_2003ministe_2003
Hi Deepak, oppList is already a List, of type Opportunity.  It's declared at the top of the class and I initialise it just before that first code chunk, simply oppList = new List<Opportunity>();  All very standard stuff.  I've never seen this error before when inserting lists of anything!
ministe_2003ministe_2003
This is the contents of the list when I run the insert.  The two elements aren't identical - the name and CloseDates are different!  Completely confused here

(Opportunity:{Industry__c=Metals, StageName=R3 - Qualified Lead, Quoted_Lead_Time__c=null, CurrencyIsoCode=GBP, Order_to_place__c=false, End_User_Company_Name__c=null, Terms_Conditions__c=null, OwnerId=00530000003mVQSAA2, Country__c=United Kingdom, Name=test fw - Sep 2014, Received_Date__c=2014-09-22 00:00:00, Quote_Required_By_Date__c=null, Date_Quoted__c=null, Booking_Entity__c=UK, AccountId=001e000000OZxLrAAL, Budget_quote__c=false, CloseDate=2014-09-22 00:00:00, Booking_Site__c=Huddersfield},

Opportunity:{Industry__c=Metals, StageName=R3 - Qualified Lead, Quoted_Lead_Time__c=null, CurrencyIsoCode=GBP, Order_to_place__c=false, End_User_Company_Name__c=null, Terms_Conditions__c=null, OwnerId=00530000003mVQSAA2, Country__c=United Kingdom, Name=test fw - Mar 2015, Received_Date__c=2014-09-22 00:00:00, Quote_Required_By_Date__c=null, Date_Quoted__c=null, Booking_Entity__c=UK, AccountId=001e000000OZxLrAAL, Budget_quote__c=false, CloseDate=2015-03-22 00:00:00, Booking_Site__c=Huddersfield})

ministe_2003ministe_2003
Never mind, turns out there was a problem somewhere between eclipse and Salesforce, and my code changes were not being saved to the server.  I'm guessing I probably fixed this bug on Friday afternoon but the code hadn't been uploaded successfully! Now on monday morning, the issue has resolved itself.