function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shanky77Shanky77 

How to delete the duplicate records from the database

Hi

Can any body tell me how to delete the duplicate record from the database.

This duplicate reocrd is judged on the basis of comparing each field of one record with

the each field of the other record.

 

I used two for loops for this . Every time I m comparing the index of 1st record with the rest of the

records then again i m jumping to the next index and coampring with the remaing record in the list.

 

This logic is failing badly since ony for 100 records total iteration becomes 100*100 . Htng governer limit

I m getting

 

"

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger prevnetContActiveMultipleTimes caused an unexpected exception, contact your administrator: prevnetContActiveMultipleTimes: execution of BeforeUpdate caused by: System.Exception: Too many script statements: 10201: Class.cleanUpContactRoleDuplicateRecord.cleanUpDuplicateRecord: line 26, column 25 "
Tell me how to delete the duplicate records from the database.

 

 

ThomasTTThomasTT
I don't know your definition of "duplicated records", but with assuming "records with the same sobject type (Account) and name", you just use Map. Map doesn't allow same keys, and O(1) to get a object with using the key. You may need a logic to determine which record of duplicated 2. If it kis just to delete, Map.values() is useful to get the all objects at the end. - ThomasTT
ThomasTTThomasTT
If you need to compare multiple fields to determine if they are duplicated, you may go with different approach. Assuming you "query" those records to get them, you put all fields to compare in Order By clause. If there is a duplicated record, it must be just after the record, so you just always check if previous one and the current one are the same or not. It's a old school, but still useful. - ThomasTT
PragadheeshwariPragadheeshwari
can you post the code,then it will be helpful for me to give suggestions
Shanky77Shanky77

Code is pasted below. Please let me know what best could be done to overcome governor limt problem

 


    contactRoleAccountList = [select Account__c,Contact__c,Start__c,Role_Played__c,End_Date__c 

                    from Contact_Role__c where Account__c != null]; 

            if(!contactRoleAccountList.isEmpty()) 

            {

                for(Integer i=0;i<contactRoleAccountList.size();i++) 

                {

                    Contact_Role__c contactRoleOld = contactRoleAccountList.get(i); 



                     for(Integer j=i+1;j<contactRoleAccountList.size();j++) 

                    {

                         Contact_Role__c contactRoleNew = contactRoleAccountList.get(j); 

                                        

                        System.debug('Test Condition1::::::'+(contactRoleOld.Contact__c == contactRoleNew.Contact__c ));         

                        System.debug('Test Condition2::::::'+(contactRoleOld.Role_Played__c == contactRoleNew.Role_Played__c));         

                        System.debug('Test Condition3::::::'+(contactRoleOld.Account__c == contactRoleNew.Account__c));         

                        System.debug('Test Condition4::::::'+((contactRoleNew.End_Date__c!=null && 

                        

                        contactRoleOld.Start__c >= contactRoleNew.Start__c &&

                        contactRoleOld.Start__c <= contactRoleNew.End_date__c &&

                        contactRoleOld.End_date__c >= contactRoleNew.Start__c &&

                        contactRoleOld.End_date__c >= contactRoleNew.End_date__c) || 

                        (contactRoleNew.End_Date__c==null && 

                        contactRoleOld.Start__c >= contactRoleNew.Start__c &&

                         contactRoleOld.End_date__c >= contactRoleNew.Start__c)));         

        

                        System.debug('**********************************************');

                        

                        if(contactRoleOld.Contact__c == contactRoleNew.Contact__c 

                        && contactRoleOld.Role_Played__c == contactRoleNew.Role_Played__c

                        && contactRoleOld.Account__c == contactRoleNew.Account__c

                        && ((contactRoleNew.End_Date__c!=null && 

                        //contactRoleOld.Start__c >= contactRoleNew.Start__c &&

                        contactRoleOld.Start__c <= contactRoleNew.End_date__c &&

                        contactRoleOld.End_date__c >= contactRoleNew.Start__c &&

                        contactRoleOld.End_date__c >= contactRoleNew.End_date__c) || 

                        (contactRoleNew.End_Date__c==null && 

                        contactRoleOld.Start__c <= contactRoleNew.Start__c ||

                        contactRoleOld.Start__c >= contactRoleNew.Start__c &&

                         contactRoleOld.End_date__c >= contactRoleNew.Start__c)))

                           {

                               if(!delListSet.contains(contactRoleNew.Id))

                                delListSet.add(contactRoleNew.Id);

                                System.debug('Size of **the list to be deleted:::::::'+delListSet.size());

                           }

                        }

                    }   

                } 

                for(Id idSet : delListSet) {

                    delList.add(idSet);

                }

                System.debug('Size of the list to be deleted:::::::'+delList.size());

                 

                try {

                System.debug(delList.size());

                Database.DeleteResult[] DR_Dels = Database.delete(delList);

                 System.debug('Deleted the Entire List');

                }

                catch (Exception e) 

                {

                   System.debug(e.getMessage());

                    System.debug('Could not delete the list');



                }

                

             }
 

}