2

I have a list of master accounts that each map to a single duplicate account. I want to merge each master with its corresponding duplicate using the merge DML operation.

I'm currently doing this in a for loop:

for (Account master : listMasters) {
    Account duplicate = mapMasterToDuplicate.get(master);

    try {
        merge master duplicate;
    } catch (DmlException e) {
        ...
    }
}

Is there a way to do this without executing DML in a loop?

I am implementing this in a batch class, and I am limiting the scope size when I execute the batch job, so I'm not too worried about exceeding governor limits. I just want to check if there's a more efficient method of accomplishing this batch merge that I couldn't find.

0

1 Answer 1

4

There is no bulk method for merging records. You have to do them one master at a time (with up to two duplicates). You can see for yourself that the Database class only has limited merge functionality. The only method signatures for merge are:

  • merge(masterRecord, duplicateId)
    Merges the specified duplicate record into the master sObject record of the same type, deleting the duplicate, and reparenting any related records. Merges only accounts, contacts, or leads.
  • merge(masterRecord, duplicateRecord)
    Merges the specified duplicate sObject record into the master sObject of the same type, deleting the duplicate, and reparenting any related records.
  • merge(masterRecord, duplicateIds)
    Merges up to two records of the same sObject type into the master sObject record, deleting the others, and reparenting any related records.
  • merge(masterRecord, duplicateRecords)
    Merges up to two records of the same object type into the master sObject record, deleting the others, and reparenting any related records.
  • merge(masterRecord, duplicateId, allOrNone)
    Merges the specified duplicate record into the master sObject record of the same type, optionally returning errors, if any, deleting the duplicate, and reparenting any related records. Merges only accounts, contacts, or leads.
  • merge(masterRecord, duplicateRecord, allOrNone)
    Merges the specified duplicate sObject record into the master sObject of the same type, optionally returning errors, if any, deleting the duplicate, and reparenting any related records.
  • merge(masterRecord, duplicateIds, allOrNone)
    Merges up to two records of the same sObject type into the master sObject record, optionally returning errors, if any, deleting the duplicates, and reparenting any related records.
  • merge(masterRecord, duplicateRecords, allOrNone)
    Merges up to two records of the same object type into the master sObject record, optionally returning errors, if any, deleting the duplicates, and reparenting any related records.

Not the answer you're looking for? Browse other questions tagged .