Newer Version Available

This content describes an older version of this product. View Latest

merge()

Merge up to three records into one.

Syntax

1MergeResult[]= connection.merge(MergeRequest[] mergeRequests);

Usage

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 set in the masterRecord of the MergeRequest, similar to a call to update.

The following limits apply to any merge request:
  • Up to 200 merge requests can be made in a single SOAP call.
  • Up to three records can be merged in a single request, including the master record. This is the same limit as the Salesforce user interface. If you have more than three records to merge, use the same master record in each request to avoid errors.
  • External ID fields cannot be used with merge().

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:

1SELECT Id FROM Contact WHERE isDeleted=true and masterRecordId != null 
2   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.

Starting with API version 15.0, if you specify a value for a field that contains a string, and the value is too big for the field, the call fails and an error is returned. In previous versions of the API the value was truncated and the call succeeded. If you wish to keep the old behavior with versions 15.0 and later, use the AllowFieldTruncationHeader SOAP header.

Note

Sample Code—Java

This sample merges one account with a master account. It creates two accounts and attaches a note to the account to merge with the master. After the merge, it writes the ID of the account that was merged and the number of child records updated, which in this case is one because the note of the merged account is moved to the master account.

1public Boolean mergeRecords() {
2   Boolean success = false;
3   // Array to hold the results
4   String[] accountIds = new String[2];
5   try {
6      // Create two accounts to merge
7      Account[] accounts = new Account[2];
8      Account masterAccount = new Account();
9      masterAccount.setName("MasterAccount");
10      masterAccount.setDescription("The Account record to merge with.");
11      accounts[0] = masterAccount;
12      Account accountToMerge = new Account();
13      accountToMerge.setName("AccountToMerge");
14      accountToMerge
15            .setDescription("The Account record that will be merged.");
16      accounts[1] = accountToMerge;
17      SaveResult[] saveResults = connection.create(accounts);
18
19      if (saveResults.length > 0) {
20         for (int i = 0; i < saveResults.length; i++) {
21            if (saveResults[i].isSuccess()) {
22               accountIds[i] = saveResults[i].getId();
23               System.out.println("Created Account ID: "
24                     + accountIds[i]);                 
25            } else {
26               // If any account is not created,
27               // print the error returned and exit
28               System.out
29                     .println("An error occurred while creating account."
30                           + " Error message: "
31                           + saveResults[i].getErrors()[0].getMessage());
32               return success;
33            }
34         }
35      }
36
37      // Set the Ids of the accounts 
38      masterAccount.setId(accountIds[0]);
39      accountToMerge.setId(accountIds[1]);
40
41      // Attach a note to the account to be merged with the master,
42      // which will get re-parented after the merge
43      Note note = new Note();
44      System.out.println("Attaching note to record " +
45            accountIds[1]);
46      note.setParentId(accountIds[1]);
47      note.setTitle("Merged Notes");
48      note.setBody("This note will be moved to the "
49            + "MasterAccount during merge");
50      SaveResult[] sRes = connection.create(new SObject[] { note });
51      if (sRes[0].isSuccess()) {
52         System.out.println("Created Note record.");
53      } else {
54         Error[] errors = sRes[0].getErrors();
55         System.out.println("Could not create Note record: "
56               + errors[0].getMessage());
57      }
58
59      // Perform the merge
60      MergeRequest mReq = new MergeRequest();
61      masterAccount.setDescription("Was merged");
62      mReq.setMasterRecord(masterAccount);
63      mReq.setRecordToMergeIds(new String[] { saveResults[1].getId() });
64      MergeResult mRes = connection.merge(new MergeRequest[] { mReq })[0];
65      
66      if (mRes.isSuccess())
67      {
68         System.out.println("Merge successful.");            
69         // Write the IDs of merged records
70         for(String mergedId : mRes.getMergedRecordIds()) {
71            System.out.println("Merged Record ID: " + mergedId);                           
72         }
73         // Write the updated child records. (In this case the note.)
74         System.out.println(
75               "Child records updated: " + mRes.getUpdatedRelatedIds().length);   
76         success = true;
77      } else {
78         System.out.println("Failed to merge records. Error message: " +
79               mRes.getErrors()[0].getMessage());
80      }
81   } catch (ConnectionException ce) {
82      ce.printStackTrace();
83   }
84   return success;
85}

Sample Code—C#

This sample merges one account with a master account. It creates two accounts and attaches a note to the account to merge with the master. After the merge, it writes the ID of the account that was merged and the number of child records updated, which in this case is one because the note of the merged account is moved to the master account.

1public Boolean mergeRecords()
2{
3   Boolean success = false;
4   // Array to hold the results
5   String[] accountIds = new String[2];
6   try
7   {
8      // Create two accounts to merge
9      Account[] accounts = new Account[2];
10      Account masterAccount = new Account();
11      masterAccount.Name = "MasterAccount";
12      masterAccount.Description = "The Account record to merge with.";
13      accounts[0] = masterAccount;
14      Account accountToMerge = new Account();
15      accountToMerge.Name = "AccountToMerge";
16      accountToMerge
17            .Description = "The Account record that will be merged.";
18      accounts[1] = accountToMerge;
19      SaveResult[] saveResults = binding.create(accounts);
20
21      if (saveResults.Length > 0)
22      {
23         for (int i = 0; i < saveResults.Length; i++)
24         {
25            if (saveResults[i].success)
26            {
27               accountIds[i] = saveResults[i].id;
28               Console.WriteLine("Created Account ID: "
29                     + accountIds[i]);
30            }
31            else
32            {
33               // If any account is not created,
34               // print the error returned and exit
35               Console.WriteLine("An error occurred while creating account."
36                           + " Error message: "
37                           + saveResults[i].errors[0].message);
38               return success;
39            }
40         }
41      }
42
43      // Set the Ids of the accounts 
44      masterAccount.Id = accountIds[0];
45      accountToMerge.Id = accountIds[1];
46
47      // Attach a note to the account to be merged with the master,
48      // which will get re-parented after the merge
49      Note note = new Note();
50      Console.WriteLine("Attaching note to record " +
51            accountIds[1]);
52      note.ParentId = accountIds[1];
53      note.Title = "Merged Notes";
54      note.Body = "This note will be moved to the "
55            + "MasterAccount during merge";
56      SaveResult[] sRes = binding.create(new sObject[] { note });
57      if (sRes[0].success)
58      {
59         Console.WriteLine("Created Note record.");
60      }
61      else
62      {
63         Error[] errors = sRes[0].errors;
64         Console.WriteLine("Could not create Note record: "
65               + errors[0].message);
66      }
67
68      // Perform the merge
69      MergeRequest mReq = new MergeRequest();
70      masterAccount.Description = "Was merged";
71      mReq.masterRecord = masterAccount;
72      mReq.recordToMergeIds = new String[] { saveResults[1].id };
73      MergeResult mRes = binding.merge(new MergeRequest[] { mReq })[0];
74
75      if (mRes.success)
76      {
77         Console.WriteLine("Merge successful.");
78         // Write the IDs of merged records
79         foreach (String mergedId in mRes.mergedRecordIds)
80         {
81            Console.WriteLine("Merged Record ID: " + mergedId);
82         }
83         // Write the updated child records. (In this case the note.)
84         Console.WriteLine(
85               "Child records updated: " + mRes.updatedRelatedIds.Length);
86         success = true;
87      }
88      else
89      {
90         Console.WriteLine("Failed to merge records. Error message: " +
91               mRes.errors[0].message);
92      }
93   }
94   catch (SoapException e)
95   {
96      Console.WriteLine("An unexpected error has occurred: " +
97                                 e.Message + "\n" + e.StackTrace);
98   }
99   return success;
100}

Arguments

This call accepts an array of MergeRequest objects. A MergeRequest object contains the following properties.

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.

Response

MergeResult[]