1

I have a list which contains duplicate entries.

List<Account> aList1=new List<Account>([select Id,Name from account where Name LIKE '%Falcon%']);
Map<String,Account> aMap=new Map<String,Account>();
for(Account a:aList1)
{
    aMap.put(a.Name,a); // remove duplicate if any
}

Now I have created a trigger on account object that prevents duplicates.

Now I want to compare all values in aMap with all records in aList2. If there are any duplicates they are removed.

List<Account> aList2=[select ID,Name from Account ORDER BY Name];
for(Account a:aList2)
{
   if(aMap.containsKey(a.Name))
       aMap.remove(a.Name);  removes duplicates from aList1
}

Database.Insert(aMap.values(),false);

But after the for Loop, aMap is empty.Please let me know.

2 Answers 2

2

Here, aMap and aList1 are subsets of aList2. You are looping through aList2 and removing all the elements in aMap.

The following statement will be true always

if(aMap.containsKey(a.Name))

And the respective map element will be removed here:

aMap.remove(a.Name);

At the end of looping, aMap will be empty.

Update :

Scenario: aList1 and aMap are in source org where as aList2 is in destination org.

Querying for all accounts in destination organization is very inefficient. You'll run into all sorts of limits (SOQL rows, Apex CPU limit etc.,). In destination org, this would be my query:

List<Account> aList2=[select ID,Name from Account WHERE Name In :aMap.keySet()];

This will give you the already existing accounts that you are trying to insert. Loop through aList2, and remove duplicate elements from aMap.

8
  • Is this an answer or a statement?
    – Eric
    Commented Sep 14, 2016 at 18:23
  • @Eric I was trying to answer why aMap is empty.
    – Avinash
    Commented Sep 14, 2016 at 18:38
  • @Avinash, aList1 is a list which is coming from an external salesforce instance (say source) into destination(another salesforce instance) by using rest api. Now I have removed duplicates from aList1. Assuming there could be some duplicates in destination, the objective is too loop through all the records of destination, check if there are duplicates, if there are then remove them and finally do a database.Insert. so while looping through all records of destination, aMap is empty. This is my query. Commented Sep 15, 2016 at 2:44
  • I would loop through all records of destination, check if there are any records that are same(duplicate) as those in aMap, if yes then override those duplicates.I hope I am clear now. Commented Sep 15, 2016 at 3:03
  • @DivyaManohar Querying for all accounts in destination organization is very inefficient. You'll run into all sorts of limits (SOQL rows, Apex CPU limit etc.,). In destination org, this would be my query: "List<Account> aList2=[select ID,Name from Account WHERE Name In :aMap.keySet()];" . This will give you the already existing accounts that you are trying to insert. Loop through aList2, and remove duplicate elements from aMap. I think the data set you are testing contains all dupes. That is the reason aMap is coming out as null.
    – Avinash
    Commented Sep 15, 2016 at 3:18
2

Maps work on a Key/Value pairs concept where the Key is always unique. If you your Map already contains a record with a duplicate Key, when you add the duplicate the original will be replaced automatically.

If all you care about is having unique Names and not the order in which they are added/removed all you would need to do is loop over your records and add them to the Map. Then you could get the values of the Map to obtain your unique records.

List<Account> aList1=new List<Account>([select Id,Name from account where Name LIKE '%Falcon%']);
Map<String,Account> aMap=new Map<String,Account>();
for(Account a:aList1)
{
    aMap.put(a.Name,a); //<-- At this point uniqueness is guaranteed.
}

List<Contact> uniqueContacts = aMap.values()// <-- Get unique Contact records.

Not the answer you're looking for? Browse other questions tagged .