Merge Statement

The merge statement merges up to three records of the same sObject type into one of the records, deleting the others, and re-parenting any related records.

Note
This DML operation does not have a matching database system method.

Syntax

mergesObjectsObject

mergesObjectsObject[]

mergesObjectID

mergesObjectID[]

The first parameter represents the master record into which the other records are to be merged. The second parameter represents the one or two other records that should be merged and then deleted. You can pass these other records into the merge statement as a single sObject record or ID, or as a list of two sObject records or IDs.

Rules and Guidelines

When merging sObject records, consider the following rules and guidelines:

For more information on merging leads, contacts and accounts, see the Salesforce online help.

Example

The following example merges two accounts named 'Acme Inc.' and 'Acme' into a single record:

List<Account> ls = new List<Account>{new Account(name='Acme Inc.'),new Account(name='Acme')};
insert ls;
Account masterAcct = [SELECT Id, Name FROM Account WHERE Name = 'Acme Inc.' LIMIT 1];
Account mergeAcct = [SELECT Id, Name FROM Account WHERE Name = 'Acme' LIMIT 1];
try {
    merge masterAcct mergeAcct;
} catch (DmlException e) {
    // Process exception here 
    
}
Note
For more information on processing DmlExceptions, see Bulk DML Exception Handling.
© Copyright 2000–2012 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.