Sub topic 13
Merging the duplicates you find
This chapter assumes you know how duplicate rules write duplicate record sets, which the previous chapter covered. It also assumes you can read basic Apex.
Duplicate rules only find duplicates. They do not remove them. Cleanup happens when someone merges the records.
What a merge does
A merge combines up to three records of the same object into one. You pick one record as the master. The other records are the losing records.
- The master record keeps its Id.
- You choose which field values survive. Where you pick a value from a losing record, it overwrites the master's value.
- Child records on the losing records, such as contacts, opportunities, and activities, are reassigned to the master.
- The losing records move to the Recycle Bin.
A merge is hard to undo. Restoring a losing record from the Recycle Bin does not unmerge the data. Treat the master choice and the field choices as final.
From duplicate set to merged record
A user can reach a merge from the duplicate alert shown on save, from the Potential Duplicates component on a record page, or from a duplicate record set. Merging needs access to all the records involved, including delete permission on the object. Merge rules differ by object. For example, contacts generally have to belong to the same account to be merged.
Choosing the master
Pick the master by data quality and connections, not by age alone.
- Prefer the record with the most related history, such as opportunities and cases.
- Prefer the record that integrations or external systems already reference by Id.
- Fill any blank master fields from the losing records.
Merging in Apex
You can merge in code with Database.merge. It takes the master record, the Id of the duplicate, and an all-or-none flag.
List<Account> accts = [
SELECT Id, Name
FROM Account
WHERE Name LIKE 'Acme%'
ORDER BY CreatedDate
LIMIT 2
];
if (accts.size() == 2) {
Account master = accts[0];
Account loser = accts[1];
Database.MergeResult result = Database.merge(master, loser.Id, false);
if (result.isSuccess()) {
System.debug('Merged into: ' + result.getId());
System.debug('Removed: ' + result.getMergedRecordIds());
} else {
for (Database.Error err : result.getErrors()) {
System.debug('Merge failed: ' + err.getMessage());
}
}
}Apex merge works on leads, contacts, accounts, and cases. The master's field values are kept. If you want a value from a losing record, copy it onto the master before you call merge. A merge fires delete triggers on the losing records and an update trigger on the master, so test your triggers with merges.
Common mistakes
- Merging records that only look alike, such as two real people who share a family email address.
- Merging before checking which record external systems reference.
- Forgetting that the duplicate record set may remain after the merge, so review whether the set still has meaningful items.
Exercise
Two Contact records are flagged as duplicates. One has 12 activities and is referenced by an external system through its Id. The other has a more complete phone number. Which should be the master, and how do you keep the better phone number?
Show answer
Make the record with 12 activities and the external Id the master. During the merge, choose the phone number from the other record so it overwrites the master's value. In Apex, copy the phone onto the master and update it before you call Database.merge.