Intermediate 20 min read

Merge

By the end of this lesson, you'll be able to:

  • Explain what merge does and the problem it solves (duplicate records)
  • Identify which record becomes the "master" in a merge
  • Recognize merge's specific limitations (record count, supported objects)

Prerequisites: "Delete and Undelete"

The problem: duplicate records

Imagine two Account records both represent "Acme Logistics" — created separately by two different people, each with their own related Contact and Opportunity records. Simply deleting one loses its related data; simply keeping both means data about the same company is split across two records. merge solves this directly.

Merging two records into one

Account masterAccount = [SELECT Id FROM Account WHERE Name = 'Acme Logistics' ORDER BY CreatedDate ASC LIMIT 1];
Account duplicateAccount = [SELECT Id FROM Account WHERE Name = 'Acme Logistics' ORDER BY CreatedDate DESC LIMIT 1];

merge masterAccount duplicateAccount;

masterAccount survives; duplicateAccount is deleted, and — critically — any Contact, Opportunity, or other related record that pointed at duplicateAccount is automatically re-pointed to masterAccount instead. Nothing related to the duplicate is silently lost.

Real limitations to know about

  • merge only works on a handful of standard objects (Account, Contact, Lead, and a few others) — it isn't a general-purpose tool for arbitrary custom objects.
  • You can merge at most 3 records into 1 master in a single statement.
  • Once merged, the operation cannot be undone the way delete/undelete can — choosing the correct master record matters, since the decision is effectively permanent.

Exercise

As a comment, explain what happens to a Contact that was related to the duplicate Account after a merge.

Show hint

Think about what merge does beyond just deleting the duplicate.

APEX

Merge Quiz

1. What happens to related records (like Contacts) pointing at the duplicate during a merge?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

merge combines duplicate records into one, keeping a chosen "master" record and re-pointing related records — the standard fix for accidental duplicates.