So, I'm having trouble inserting a List, since it gives me the " Before Insert or Upsert list must not have two identically equal elements".

EDIT: SOLVED: removed code as it's 'live' code, but the jist of it is: Are you having issues when adding records to a List, and on Insert, it says you're getting dupes?

I was searching for an Account to add to a list. When I added it, I was not refreshing the "Search List" - just selecting it over and over, so literally adding the same record to the list.

I had to re-run the search after I added it to the list, so it would refresh and give me a new reference for the same Account.

  • 2
    What exactly is your question? And what is wrapSearch? Can you post your whole class, this doesn't look like it should compile as it is... May 28, 2015 at 18:26
  • It's not making any sense. Could you please post the whole code here? May 28, 2015 at 18:32
  • Sure - pasting code! Thanks in advance. Please don't mind all my comments (helps me think)
    – RC82
    May 28, 2015 at 18:50

1 Answer 1


When you receive your list, or before you sent it to your class, do something like this:

set<wrapPropObj>selectedset = new set<wrapPropObj>();


This will remove all the duplicate Ids. You can then run selectedset through your method without problems. If you do it before you send it to your class, you can use the addall method again to put it back into a list.

  • So this will clear the list of duplicates, correct? So if 2 duplicates in a list of 5, I'll have 3 records remaining? <br> The thing is, I want the same records inserted multiple times, just with different IDs. Does this help here too?
    – RC82
    May 28, 2015 at 18:53
  • You cannot have same record ID multiple times. That's what your error message is telling you. Putting them into a set removes any duplicate Ids. Now if it's the same field/property, then presumably it would change it for the object if it had been selected multiple times, but I don't know the full context of what you're doing, only that it will remove duplicate ID's. I now see you've added a LOT of code since my original answer was posted. Would need to study it to say more.
    – crmprogdev
    May 28, 2015 at 19:00
  • Great, thanks for the help! I'm going to try it in the next hour or so. I'll update - I really appreciate you taking the time to help!!
    – RC82
    May 28, 2015 at 19:07
  • @crmprogdev If its a custom object you are adding to the set equals and hashCode methods will be needed.
    – Keith C
    May 28, 2015 at 19:47
  • @KeithC. Understand. Code has changed considerably from what began as a very simple initial post. That's what happens in these wrapper classes and it's also why the issue is about repetitive object ids.
    – crmprogdev
    May 28, 2015 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.