Search Here

Apex Merge custom object DML

 In Apex, the merge statement is used to combine up to three duplicate records into a single "master" record. 

This operation is typically performed on standard objects, such as Leads, Contacts, and Accounts, to eliminate

 duplicate data. Merging custom objects is not directly supported using the standard merge statement.


Here's an example of how to use the merge statement with a standard object, such as a Contact:


List<Contact> contactsToMerge = [SELECT Id FROM Contact WHERE Email = 'duplicate@example.com' LIMIT 3];


if (contactsToMerge.size() > 1) {

    // Perform the merge operation

    merge contactsToMerge[0];

}


We query a list of Contact records based on a specified condition (email address) using SOQL.


We check if there are at least two duplicate Contact records in the contactsToMerge list. To perform a merge, you need at least two duplicate records.


If there are multiple duplicate records, we use the merge statement to combine the duplicate records into a single master record. The first record in the list (contactsToMerge[0]) becomes the master record, and the other duplicate records are merged into it.




Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.