Merge up to three records into one.
mergeResult[]= sfdc.merge(mergeRequest[] mergeRequests);
Use this call to merge records of the same object type into one of the records, deleting the others, and re-parenting any related records. Each merge operation is within one transaction. A batch merge has multiple transactions, one for each element in the batch.
The only supported object types are Lead, Contact and Account.
The masterRecord field indicates which of the records is the master record that the others are merged into. You can use queryAll() to view records that have been deleted during a merge.
This call requires that you decide prior to the merge call if there are any field values from the non-master record(s) that should supersede the values in the master record. If so, the field names and their new values should be provided in the masterRecord of the MergeRequest, similar to a call to update.
To find all records that have been merged since a given point in time, you can use queryAll() with a SELECT statement similar to the following:
SELECT Id, FROM Contact WHERE isDeleted=true and masterRecordId != null
AND SystemModstamp > 2006-01-01T23:01:01+01:00
It is a recommended best practice to narrow your result set to the most relevant records by filtering on SystemModstamp.
Account masterAccount = new Account();
masterAccount.setName("MasterAccount");
SaveResult masterAccountSaveResult = binding.create(new SObject[] {masterAccount})[0];
masterAccount.setId(masterAccountSaveResult.getId());
masterAccount.setDescription("Old description");
Account accountToMerge = new Account();
accountToMerge.setName("AccountToMerge");
accountToMerge.setDescription("Duplicate account");
SaveResult accountToMergeSaveResult = binding.create(new SObject[] {accountToMerge})[0];
// Attach a note, which will get re-parented
Note note = new Note();
note.setParentId(accountToMergeSaveResult.getId());
note.setBody("This note will be moved to the MasterAccount during merge");
binding.create(new SObject[] {note});
MergeRequest mr = new MergeRequest();
// Perform an update on the master record as part of the merge:
masterAccount.setDescription("Was merged");
mr.setMasterRecord(masterAccount);
mr.setRecordToMergeIds(new ID[] {accountToMergeSaveResult.getId()});
MergeResult result = binding.merge(new MergeRequest[] {mr})[0];
System.out.println("Merged " + result.isSuccess() + " got " +
result.getUpdatedRelatedIds().length + " updated child records");
The following sample illustrates how to merge three accounts:
Account master = new Account(name='Master');insert d;
Account d = new Account(name='MergeD');insert d;
Account e = new Account(name='MergeE');insert e;
merge master new String[] {a.id};
merge master new Account[] {d, e};
private void mergeSample()
{
try
{
Account masterAccount = new Account();
masterAccount.Name = "MasterAccount";
SaveResult sr = binding.create(new sObject[] { masterAccount })[0];
masterAccount.Id = sr.id;
masterAccount.Description = "Old description";
Account accountToMerge = new Account();
accountToMerge.Name = "AccountToMerge";
accountToMerge.Description = "Duplicte account";
SaveResult accountToMergeSaveResult = binding.create(new sObject[]
{ accountToMerge })[0];
// Attach a note, which will get re-parented
Note note = new Note();
note.ParentId = accountToMergeSaveResult.id;
note.Body = "This note will be moved ot the MasterAccount during merge";
binding.create(new sObject[] { note });
MergeRequest mr = new MergeRequest();
// Perform an update on the master record as part of the merge:
masterAccount.Description = "Was merged";
mr.masterRecord = masterAccount;
mr.recordToMergeIds = new String[] { accountToMergeSaveResult.id };
MergeResult result = binding.merge(new MergeRequest[] { mr })[0];
Console.WriteLine("Merged " + result.success + " got " + result.ToString()
+ " updated child records");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
| Name | Type | Description |
|---|---|---|
| masterRecord | sObject | Required. Must provide the ID of the object that other records will be merged into. Optionally, provide the fields to be updated and their values. |
| recordToMergeIds | ID[] | Required. Minimum of one, maximum of two. The other record or records to be merged into the master record. |
mergeResult[]