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

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 "
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.
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');
}
}
}