0

I am working on deleting duplicate child records on contact merge.

Example: I have a contact C1 and C2 : (C1 has A,B,C records and C2 has A,D,E) records and now record A is the duplicate with same name. On merge I have to delete one of the record and that is conditionally based( Means A validate checkbox on C1 is 'TRUE', A validate checkbox on C2 is 'FALSE') I need to delete C2 here.

For the same I am able to get these duplicate records in a list and now i tried below code, but it is not working.

// getting duplicate records here...........
deleteList = [SELECT Id,Name validate__c FROM childobject WHERE Name IN : duplicatelist];
Set<string> DuplicateCheck = new Set<string>();
Set<Id> DupeIds = new Set<Id>();

for(childobject pcc: deleteList){
    // How do we validate records based on my checkbox here in if condition........
    if(!DuplicateCheck.add(pcc.Name))
        // this if condition is removing duplicate, but i should select which one to remove from duplicates
        DupeIds.add(pcc.Id);
                               
}

// Adding duplicate records to this to delete
List<childobject>  deleteList = [select Id
                      FROM childobject 
                      WHERE Id IN : DupeIds ];
delete deleteList;
2
  • How the validate checkbox in the contact is checked ?. Is it any automation already available for that?
    – Raj
    Oct 16, 2021 at 6:08
  • They will be selected manually by users.
    – Bhanu
    Oct 17, 2021 at 17:07

1 Answer 1

0

Please fix the Validate__c field on SOQL query in your code and use that field to identify the duplicate records.

List<child_object__c> deleteList = new List<child_object__c>();
//To find duplicate child records use Validate__c field in WHERE condition
deleteList = [SELECT Id, Name, Contact.validate__c FROM child_object__c WHERE Contact.validate__c = False];
//Initiate the delete process
Delete deleteList;

Since as you mentioned, Validate check box in Contactrecord is already enabled by end user. So please run your apex code via batch in week or Everyday cycle.

Please let me know if it helps.

You must log in to answer this question.

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