0

I wanted to remove records with duplicate Reporting_Year__c in a lost.

I have got the code and debug shows duplicate removed. but when I test I can see all records inc duplicate

list<Funding_Cycles_Contacts__c>  Uniquefcc = [Select id, Reporting_Year__c,Reporting_Year_End__c,
                                                           Achieved_Credits_in_Reporting_Yr_Funded__c,
                                                           Funded_Months__c, Contact__c,contact__r.FCC_Count__c
                                                           from Funding_Cycles_Contacts__c 
                                                           where Contact__c IN: updatedContId
                                                           Order by Reporting_Year__c];
    for(integer i=0;i<Uniquefcc.size();i++) {
             Funding_Cycles_Contacts__c fcci = Uniquefcc.get(i);

         for(integer j= i;j<Uniquefcc.size();j++) {
             Funding_Cycles_Contacts__c fccj = Uniquefcc.get(j); 
             if(fcci.Reporting_Year__c == fccj.Reporting_Year__c && i!=j) {
                 Uniquefcc.remove(j); 
                 system.debug('REMOVED::'+fcci.Reporting_Year__c);  
             }
         }
     }
    update Uniquefcc;

  system.debug('========final list=====' + Uniquefcc );
6
  • Using an O(n^2) algorithm has serious performance implications. You'll want to use a Map instead. That said, this algorithm should technically work. Also, comparing the entire record using == or != is a Bad Idea, as is using List.get, and repeated List.size calls. Finally, you're potentially skipping records, as you'd want to use Uniquefcc.remove(j--);
    – sfdcfox
    Jun 21, 2017 at 1:56
  • I used a map with a Date as Key as below
    – Jay Weera
    Jun 21, 2017 at 3:05
  • I used a map as the Date as Key and List as the value and trying to put the values in the map then error thrown as Ill-legal assignment of sobject to List Map<Date,Funding_Cycles_Contacts__c> UniqueMap = new Map<Date,Funding_Cycles_Contacts__c>();
    – Jay Weera
    Jun 21, 2017 at 3:10
  • this is how for(Funding_Cycles_Contacts__c newfcc: fccNewRd){ list<Funding_Cycles_Contacts__c> listMap = UniqueMap.get(newfcc.Reporting_Year__c); if(listMap==Null){ listMap = new List<Funding_Cycles_Contacts__c>(); UniqueMap.put(newfcc.Reporting_Year__c, listMap); } listMap.add(newfcc); }
    – Jay Weera
    Jun 21, 2017 at 3:11
  • @JayWeera Is Reporting_Year__c unique field you are using to remove the duplicates?
    – Reshma
    Jun 21, 2017 at 6:26

3 Answers 3

-1

If you want to delete duplicate record then you have first find duplicate records and stored in list then delete all records from org.. Use following code to remove duplicate rec

list<Funding_Cycles_Contacts__c>  Uniquefcc = [Select id, Reporting_Year__c,Contact__c
                                           from Funding_Cycles_Contacts__c 
                                           where Contact__c IN: updatedContId
                                           Order by Reporting_Year__c];

map yearVsFCCRecordMap = new map(); list removeList = new list(); for(Funding_Cycles_Contacts__c FCCObj : Uniquefcc){

if(yearVsFCCRecordMap.containskey(FCCObj.Reporting_Year__c)){

    removeList.add(FCCObj);
}
else{

    yearVsFCCRecordMap.put(FCCObj.Reporting_Year__c, FCCObj);
}

} if(removeList.size()>0){

database.delete(removeList, false);

}

Thank you, Amol Salve salesForce Developer

1
  • Sorry, I got it wrong now I am having a compile error Incompatible value type Funding_Cycles_Contacts__c for Map<Date,List<Funding_Cycles_Contacts__c>> on the line yearVsFCCRecordMap.put(FCCObj.Reporting_Year__c, FCCObj); thanks
    – Jay Weera
    Jun 25, 2017 at 8:19
0

Use set instead of list to remove duplicate or you can use

> map<integer,object>

so integer would be your Reporting_Year__c and object would be your Contact__c

0

You can remove duplicate record using Map .

Apex code:

Map<Date,Funding_Cycles_Contacts__c> objMap = new Map<Date,Funding_Cycles_Contacts__c>();

list<Funding_Cycles_Contacts__c>  Uniquefcc = [Select id, Reporting_Year__c,Reporting_Year_End__c,
                                                       Achieved_Credits_in_Reporting_Yr_Funded__c,
                                                       Funded_Months__c, Contact__c,contact__r.FCC_Count__c
                                                       from Funding_Cycles_Contacts__c 
                                                       where Contact__c IN: updatedContId
                                                       Order by Reporting_Year__c];


for(Funding_Cycles_Contacts__c FCC :Uniquefcc ){
    if(!objMap.containsKey(FCC.Reporting_Year__c)){
        objMap.put(FCC.Reporting_Year__c,FCC); 
    }

}
update objMap;

You must log in to answer this question.

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