+ Start a Discussion
TehNrdTehNrd 

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

This makes absolutely no sense to me. I am trying to insert two OpportunityLineItems that are exactly the same but I get this exception. What good reason is there to stop the insertion of two Records that are the same?

I understand if this was an upsert operation as you can't insert and update and the same time but I am inserting two brand new OpportuntiyLineItem records.

Thanks,
Jason


Message Edited by TehNrd on 09-24-2008 04:48 PM
Best Answer chosen by Admin (Salesforce Developers) 
TehNrdTehNrd
Gah!

Figure it out. I had to redo some of my code and I added a loop to the code and then forgot to put my opp initiation statement in the loop. So really it was not two records that were the same but one record in the List twice.

Here is the nitty gritty.
Code:
//I would define a new List

List<Opportunity> opps = new List<Opportunity>();

//I had to change my code to add a loop to populate a list of objects and this is what it looked like:

Opportunity opp = new Opportunity();
for(SomeotherObject 0 : collection){
 opp.amount = 50;
 opps.add(opp);
} //Problem with this is that I needed to add the opp initialization to within the for loop: //Here is the good way
for(SomeotherObject 0 : collection){ Opportunity opp = new Opportunity(); opp.amount = 50; oops.add(opp);
}

Sigh.....I wasted too much time trying to figure this out :smileysad:.
 


Message Edited by TehNrd on 09-24-2008 05:13 PM

All Answers

TehNrdTehNrd
Gah!

Figure it out. I had to redo some of my code and I added a loop to the code and then forgot to put my opp initiation statement in the loop. So really it was not two records that were the same but one record in the List twice.

Here is the nitty gritty.
Code:
//I would define a new List

List<Opportunity> opps = new List<Opportunity>();

//I had to change my code to add a loop to populate a list of objects and this is what it looked like:

Opportunity opp = new Opportunity();
for(SomeotherObject 0 : collection){
 opp.amount = 50;
 opps.add(opp);
} //Problem with this is that I needed to add the opp initialization to within the for loop: //Here is the good way
for(SomeotherObject 0 : collection){ Opportunity opp = new Opportunity(); opp.amount = 50; oops.add(opp);
}

Sigh.....I wasted too much time trying to figure this out :smileysad:.
 


Message Edited by TehNrd on 09-24-2008 05:13 PM
This was selected as the best answer
jkucerajkucera
I appreciate your time!  Helped me figure out a problem tonight testing out batch apex.
Manu ErwinManu Erwin

Thanks for this - helped me work out where I had gone wrong.

 :)

Martha_SenetaMartha_Seneta

Thanks - this helped me too.

success22success22

Thank you so much. It helped me too.. N saved my lots of time

raja1986raja1986

Just Awsome. Thank you for the fix provided :)

neeedhelpneeedhelp
Awesomee.Added +1 for your post
Lalit_RautelaLalit_Rautela

Hi,

 

If we have more than one record than only last record will save in the list.if we initialized list under the for loop .

its not working when we have more than one record . 

 

neeedhelpneeedhelp

you have to Insert the Records outside the for loop.Can you post the code once so that it will be more clear where you went wrong

devsfdc@cloud.com .devsfdc@cloud.com .
It helped me too. It got clarity on inserting, Thanks for the info...
Karl TschaepeKarl Tschaepe
I did the exact same thing. Thank you so much for posting your solution.
Mustafa JhabuawalaMustafa Jhabuawala
Iniating object in the for loop actually worked. NICE!.....

But why this is happening what is the exact reason, it should work if I am initiating it outside the loop
cloudgofercloudgofer
super!! thank you , it helped me to catch the mistake I was making.
Kharla HaldosKharla Haldos
Thanks for posting this very helpful
Vishnu Kant PandeyVishnu Kant Pandey
What should i do when i am having so many child records and need to show them on parent level. As i tried the given way but duplicate records are getting inserted in my case?
Swapnil GuravSwapnil Gurav
I was also facing the same issue. Thanks for the info. :) 
Michael MMichael M
This helped me today, had the same issue, thank you!