• kibitzer2
  • NEWBIE
  • 0 Points
  • Member since 2014
  • CTO
  • Full Circle CRM


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
I seem to run into this situation a lot and have been using a brute force way around it.  For example, I query a bunch of contacts for their accounts and put the results into a list.  But this list has duplicate accounts.  I want the list de-duped.  I've tried using the GROUP BY clause of the query but the query results are then aggregate result objects that I have to convert into the object in question.

I've settled on this code which is pretty concise but is a shotgun approach.  I'm thinking there has to be an easier way (using Maps?).  Any comments?

Thanks!!

for (Opportunity anOpp1 : FullListOfOpps) {
        FOUND = false;  // boolean
        for (Opportunity anOpp2 : ListOfOppsWithoutDuplicates) {
           if (anOpp2.Id == anOpp1.Id) {
             FOUND = true;
           }
        }
        if (!FOUND) {
           Opportunity newOpp = new Opportunity();      
           newOpp.id = anOpp1.id;
           ListOfOppsWithoutDuplicates.add(newOpp);
        }
      }