# How does the account merge mechanism consolidate duplicate enterprise accounts and associated child records?

**Topic:** Sales Cloud · **Difficulty:** Medium · **Tags:** Sales Cloud, Account Merge, Data Hygiene

Read it on the site: https://www.forcechallenges.com/interview-questions/how-does-the-account-merge-mechanism-consolidate-duplicate-enterprise-accounts-and-associated-child-records

## Answer

**Basics:** Duplicates split activity history and opportunities across records. Merging picks a master, lets you choose the winning value for each conflicting field, and moves related contacts, opportunities, cases, notes, attachments and activities to the master.

**How it works:**
1. Find duplicates (Duplicate Rules, matching rules, or reports).
2. In Lightning, use Merge Accounts from the account list (select up to three) or the Duplicate Record Sets.
3. Choose the master; select the field values to keep.
4. Confirm; related records are re-parented, child accounts in the hierarchy move under the master, and losing accounts are deleted (recoverable from the Recycle Bin for 15 days).
5. Duplicate rule alerts and merge jobs (where licensed) can help at scale.

**Advanced / deep dive:** Programmatic merge supports a master and up to two losers per statement. Triggers fire: delete triggers on the losers, update on the master, with MasterRecordId visible in before/after delete context, which integrations must handle to re-key external systems. Sharing: merged related records follow the master's sharing. Some situations restrict merging (for example Person Accounts only merge with Person Accounts; portal or partner users, and accounts with certain features, have limits; verify in current docs). Prevention is better: use matching rules and duplicate rules to block or alert at entry, and use Duplicate Jobs (edition-dependent) for cleanup. For massive cleanups, plan batches of merges and avoid sharing recalculation during business hours.

**Code / config example:**
```apex
Account master = [SELECT Id FROM Account WHERE Id = :masterId];
List<Id> losers = new List<Id>{ dup1Id, dup2Id };   // max two losers
Database.MergeResult[] results = Database.merge(master, losers, false);
for (Database.MergeResult r : results) {
    if (!r.isSuccess()) { System.debug(r.getErrors()[0].getMessage()); }
}
```

**Hidden gotchas & must-remember points:**
- The maximum is three records per merge (one master and two others).
- Losing records' field values are lost unless you pick them.
- Merged-away IDs still appear in integrations; use MasterRecordId to remap.
- The Recycle Bin keeps deleted records for 15 days.
- Merge is not a bulk tool; each merge is a separate transaction with locks.
- Activities, notes and attachments move, but custom unrelated lookups to the loser may be re-pointed only for supported relationships.

**Best practice:** Prevent duplicates with matching and duplicate rules, and treat merge as cleanup, backed up and logged.

---
Source: ForceChallenges · last updated 2026-11-02
