ApexSensei

Drills DML operations

Merge duplicate Accounts

DML operations · Intermediate · 14 min · Runs in your Salesforce Developer org · Apex Path

`Database.merge(master, duplicate)` keeps the master row, deletes the duplicate, and moves related children to the master. The duplicate goes to the Recycle Bin. Keep one tagged Account as the master, merge a duplicate into it, and prove the loser is in the bin.

Lesson

Why this matters

Cleanup jobs run into two records for the same customer. You could delete one, but then the children (Contacts, Opportunities, Cases) go with it. merge is the right tool: the master stays, its children keep their parent, the duplicate's children are re-pointed at the master, and the duplicate moves to the Recycle Bin.

The smallest working example

Account master    = new Account(Name = 'Acme');
Account duplicate = new Account(Name = 'Acme Copy');
insert master;
insert duplicate;

Database.merge(master, duplicate);

After the merge, only master.Id is active. duplicate.Id is in the Recycle Bin.

Name the parts

  • **Database.merge(master, duplicate)** keeps the first argument and deletes the second. Related child rows move to the master.
  • The master can be an sObject or just an Id. The duplicate can be an sObject or a list of up to two Ids.
  • The loser's Id can still be queried with ALL ROWS because it is in the Recycle Bin.
  • The master's own fields win on conflicts, unless you set a field on the master before calling merge.

A common wrong version

delete duplicate;

A plain delete removes the duplicate, but its Contacts, Opportunities and Cases go with it. That's data loss, not a merge.

The fix

Use Database.merge so children follow the surviving record:

Database.merge(master, duplicate);

Now you try

Starter inserts both Accounts. Call Database.merge(master, duplicate), count how many of the two Ids are still active, confirm the remaining row is the master, and count the duplicate's Id with IsDeleted = true ALL ROWS. Print Active after merge:, Master kept: and Duplicate in bin: in that order.

Learn more

The challenge

Merge the starter's duplicate Account into the master Account and prove where each ends up.

Examples:

  • After merge, a count by both Ids → Active after merge: 1
  • The remaining row has the master's Name → Master kept: true
  • The duplicate is only visible with ALL ROWS → Duplicate in bin: 1

Requirements:

  1. Database.merge should be present with master first.
  2. Active after merge: 1 should be printed from a count by both Ids.
  3. Master kept: true should be printed from a count on the master's Id and Name.
  4. Duplicate in bin: 1 should be printed from an ALL ROWS count on the duplicate's Id.

Starter code

String tag = 'AS-MRG-' + UserInfo.getUserId() + '-' +
    String.valueOf(Datetime.now().getTime());
Account master = new Account(Name = tag + '-Master');
Account duplicate = new Account(Name = tag + '-Copy');
insert master;
insert duplicate;

Related drills

Try this drill free

You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.

‹ Roll back parent and children on a child failure