You need to sign in to do that
Don't have an account?

issue with duplicate values in a list
Hi,
I the below code, insertlstTarget is having duplicate values. How do i elminate them?
Thanks
I the below code, insertlstTarget is having duplicate values. How do i elminate them?
For(Course__c course1 : lstTarget){ Course__c course2 = new Course__c (); course2 = course1.clone(false,false,false,false); course2.Specialisation_Code__c = ''; course2.Specialisation__c = ''; course2.Course_Code__c = course1.Name; newLstTarget.add(course2); system.debug('newLstTarget' + newLstTarget.size()); } Map<String,Course__c> extIdMap1 = new Map<String,Course__c>(); for(Course__c so1 : newLstTarget){ extIdMap1.put((String)so1.get('Course_Code__c'),so1); } Set<String> extIdSet1 = extIdMap1.keyset(); String splitQuery1 = 'SELECT Id,Course_Code__c FROM Course__c WHERE Course_Code__c in :extIdSet1'; system.debug(splitQuery1); List<Course__c> insertlstTarget = new List<Course__c>(); Map<Id,Course__c> updateMap1 = new Map<Id,Course__c>(); Map<String,Id> queryMap1 = new Map<String,Id>(); for(Course__c so1 : database.query(splitquery1)){ queryMap1.put((String)so1.get('Course_Code__c'), so1.Id); } for(Course__c so1 : newLstTarget){ Id theId = queryMap1.get((String)so1.get('Course_Code__c')); if(theId != null){ so1.Id = theId; updateMap1.put(theId,so1); } else{ insertlstTarget.add(so1); } }
Thanks
Hi,
Sets, doesn't allow duplicates.
Set<sobject> myset = new Set<sobject>();
List<sobject> result = new List<sobject>();
myset.addAll(originalList);
result.addAll(myset);
Please try the above one. I hope its works fine.
Thanks,
Saroja
SweetPotatoTec
All Answers
Refer below link.
http://www.infallibletechie.com/2014/04/how-to-remove-duplicates-from-list-in.html (http://http://www.infallibletechie.com/2014/04/how-to-remove-duplicates-from-list-in.html)
Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
Best Regards
Sandhya
use set instead of a list.
Hi,
Sets, doesn't allow duplicates.
Set<sobject> myset = new Set<sobject>();
List<sobject> result = new List<sobject>();
myset.addAll(originalList);
result.addAll(myset);
Please try the above one. I hope its works fine.
Thanks,
Saroja
SweetPotatoTec