You need to sign in to do that
Don't have an account?
Sumesh Chandran
Remove duplicates from List
I am trying to remove duplicates from a list. The list has duplicates in the city column and the province column.
This is what I tried according to what was mentioned on this post https://developer.salesforce.com/forums/?id=906F00000008y3SIAQ
It doesn't work for me.
This is what I tried according to what was mentioned on this post https://developer.salesforce.com/forums/?id=906F00000008y3SIAQ
It doesn't work for me.
global void execute(Database.BatchableContext BC, List<MDU_Squad_Data_min__c> cities) { Set<MDU_Squad_Data_min__c> citySet = new Set<MDU_Squad_Data_min__c>(); citySet.addAll(cities); List<MDU_Squad_Data_min__c> noDuplicateCityList = new List<MDU_Squad_Data_min__c>(citySet); }Please advise!
All Answers
Try the below code and let me know.
Thanks,
Maharajan.C
Thanks,
Maharajan.C
Lets say we create 3 Account records who all have the same name, annual revenue but different industries:
Account acc1 = new Account(Name= 'Test Account', AnnualRevenue = 500, Industry = 'Apparel');
Account acc2 = new Account(Name= 'Test Account', AnnualRevenue = 500, Industry = 'Banking');
Account acc3 = new Account(Name= 'Test Account', AnnualRevenue = 500, Industry = 'Agriculture');
Now lets add these to a List called accList:
List <Account> accList = new List <Account>();
accList.add(acc1); accList.add(acc2); accList.add(acc3);
Now lets put this list into a set called accSet by passing it as an argument.
Set <Account> accSet = new Set <Account>(accList);
Now if I was to do System.debug(accSet); you'll notice that the output contains the same records as the list. As mentioned before, even though each record had the same name and Annual Revenue, they had some other field which didn't match causing the set to consider each of them unique.
Possible Solutions:
Possible Solution #1: Reduce the amount of fields you are using in your query to only those that are relevant. For example, assuming you are using Database.QueryLocator in your start method, only state the Name and sumchans_Province__c in your query. This should resolve the issue.
Possible Solution #2: If the other fields in your query are required, then copy and paste the code I have written below into the curly brackets of the execute method.
Overall, this was quite fun to troubleshoot and I hope my solution helps! If you have any questions, feel free to leave a comment or message me.
Code Starts:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
List <MDU_Squad_Data_min__c> noDuplicateCityList = new List <MDU_Squad_Data_min__c>();
Map <String, MDU_Squad_Data_min__c> mduMap = new Map <String, MDU_Squad_Data_min__c>();
for (integer i = 0; i < cities.size(); i++) {
mduMap.put(cities[i].Name, cities[i]); // I assume ‘Name’ is what stores the city name. If not please change ’Name’ to the correct API name.
}
Set <String> mduSet = new Set <String>(mduMap.keyset());
List <String> mduSetList = new List <String>(mduSet);
for (integer i = 0; i < mduSetList.size(); i++) {
noDuplicateCityList.add(mduMap.get(mduSetList[i]));
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Code Ends!
I tried your code, it still doesn't get rid of the duplicates.
The solution 1 you mentioned, this what I had in my start method since the beginning, I was not pulling in any other fields.
Also attaching the whole code here, if you could find, if I am doing something wrong somewhere.
Thanks again!
Also, I am curious as to what the result would be if we were to filter out the cities list first before creating the sumchans__City_Master__c objects out of it. I don't think it'll make much of a difference but it is worth a try.