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
DevelopementDevelopement 

Merge Duplicate Records

Hello,

Can anyone help to write a logic to merge duplicate records of Custom Object?

 

Thanks,

Shruti

Arun MKArun MK


Use the merge statement to achieve this functionality.

 

Find the sample code below:

 

List<CustomObject__c> ls = new List<CustomObject__c>{new CustomObject__c(name='Sample Name 123'), new CustomObject__c(name='Sample Name 456')};


insert ls;


CustomObject__c masterObj = [SELECT Id, Name FROM CustomObject__c WHERE Name = 'Sample Name 123' LIMIT 1];
CustomObject__c mergeObj = [SELECT Id, Name FROM CustomObject__c WHERE Name = 'Sample Name 456' LIMIT 1];

merge masterObj mergeObj;

 


The above code will merge the mergeObj to the masterObj record.

 

Regards,

Arun.

vbsvbs
@Arun - As per SF docs the merge is only supported by Leads, Accounts and Contacts hence I guess the request from OP.
@Dev - I guess you would have to replicate the standard merge would have worked. The general logic to flow would be:
1. Check if master record has no value for a specified column then overwrite value from merged record
2. Reparent all children to master record
3. Delete merge records.

This can be made dynamic by using Schema describe and getting fields for the specific object and doing a one-to-one compare for the above logic.