merge()
同じ種別の最大 3 つのレコードを 1 つのレコードに統合します。入力は MergeRequest 要素の配列で、各要素は統合するレコードを指定します。出力は、マージ結果に関する情報を含む MergeResult オブジェクトです。
構文
1MergeResult[]= connection.merge(MergeRequest[] mergeRequests);使用方法
merge() を使用して、同じオブジェクト種別のレコードを、主レコードと呼ばれる 1 つのレコードに統合します。merge() は、ビクティムレコードと呼ばれる他のレコードを削除します。ビクティムレコードに関連レコードがある場合、merge() は主レコードを関連レコードの新しい親にします。
ルールとガイドライン
- 非主レコードの値
- merge() をコールする前に、非主レコードの項目値を主レコード値よりも優先するかどうかを決定します。優先する場合は、MergeRequest の masterRecord によって識別されるレコードの項目名と値を設定します。
- 取引先責任者、リード、データプライバシーレコード
- 個人オブジェクトに基づいた対応するデータプライバシーレコードがある取引先責任者またはリードをマージする場合、merge() は主レコードに関連付ける適切なデータプライバシーレコードを決定します。
- リードと取引先責任者のマージに対して最後に更新されたデータプライバシーレコードを保持するオプションを選択した場合、merge() は最後に更新されたデータプライバシーレコードを選択します。
- それ以外の場合、merge() はすでに主レコードに関連付けられているデータプライバシーレコードを選択します。
- 連続するマージ
-
merge() は入力引数の各 MergeResult 要素を別個のトランザクションとして処理するため、複数のレコードを連続して同じ主レコードにマージできます。
連続するマージを実行するには、MergeResult 要素の配列を指定して merge() をコールします。MergeResult 要素ごとに、以下を設定します。
- masterRecord を主レコード ID に設定します。
- recordToMergeIds 配列の各要素を、主レコードに統合するレコードの ID に設定します。
- 削除されたレコード
- queryAll() を使用して、マージ中に削除されたレコードを表示します。
- マージされたレコードの表示
- 指定の時間内にマージされたすべてのレコードを見つけるには、SELECT ステートメントと一緒に queryAll() をコールします。次に例を示します。
1SELECT Id FROM Contact WHERE isDeleted=true and masterRecordId != null 2 AND SystemModstamp > 2006-01-01T23:01:01+01:00 - サポートされているオブジェクト種別
-
サポートされているオブジェクト種別は、Lead、Contact、Account、Person Account、および Individual です。同じ種別のオブジェクトのみをマージできます。たとえば、リードはリードとのみマージできます。
- 関連取引先
- 関連取引先の一部である取引先をマージする場合、merge() はビクティムの子レコードを主レコードの子として設定しようとします。このアクションによって循環的なリレーションが生まれる場合、merge() はエラーを返します。
- 取引先責任者の上司リレーション
- ReportsToId 項目に値がある取引先責任者をマージする場合、merge() はビクティムの ReportsToId 値を主レコードに追加しようとします。このアクションによって循環的なリレーションが生まれる場合、merge() はエラーを報告します。
- 取引先責任者とポータルユーザー
- ポータルユーザーが関連付けられている取引先責任者ビクティムレコードをマージする場合、ビクティムレコードの MergeRequest 要素の AdditionalInformationMap を設定します。1 つのビクティムとポータルユーザーのみ主レコードにマージできます。Salesforce Classic では、カスタマーポータルの使用が有効になっている個人取引先をマージできません。
- 考慮事項
- 次の制限が、マージ要求に適用されます。
- 1 つの SOAP コール内に、最大 200 件のマージ要求を作成可能です。
- 主レコードを含めて、最大 3 つのレコードが 1 つの要求にマージ可能です。この制限は、Salesforce ユーザーインターフェースによって適用される制限と同じです。3 件を超えるレコードをマージするには、連続するマージを実行します。
- 外部 ID 項目では、merge() を使用することはできません。
- リードと取引先責任者のマージに対して最後に更新されたデータプライバシーレコードを保持するオプションを選択し、選択されたデータプライバシーレコードの CRUD 権限がコール元にない場合、メインレコードにすでに関連付けられているデータプライバシーレコードがマージプロセスにより選択されます。
- 重複するリレーション
- 同じ取引先責任者に関連付けられている取引先または個人取引先はマージできません。取引先をマージする前に、取引先と取引先責任者の重複リレーションを削除します。
サンプルコード — Java
このサンプルでは、1 つのビクティム取引先を主取引先とマージします。2 つの取引先が作成され、ビクティムにメモが添付されます。マージ後、コードはビクティム取引先の ID と更新された子レコード数を出力します。この例では、マージされた取引先のメモは主レコードに移動するため、更新されたレコード数は 1 です。
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}サンプルコード — C#
このサンプルでは、1 つのビクティム取引先を主取引先とマージします。2 つの取引先が作成され、ビクティムにメモが添付されます。マージ後、コードはビクティム取引先の ID と更新された子レコード数を出力します。この例では、マージされた取引先のメモは主レコードに移動するため、更新されたレコード数は 1 です。
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
74 MergeResult mRes = binding.merge(new MergeRequest[] { mReq })[0];
75
76 if (mRes.success)
77 {
78 Console.WriteLine("Merge successful.");
79 // Write the IDs of merged records
80 foreach (String mergedId in mRes.mergedRecordIds)
81 {
82 Console.WriteLine("Merged Record ID: " + mergedId);
83 }
84 // Write the updated child records. (In this case the note.)
85 Console.WriteLine(
86 "Child records updated: " + mRes.updatedRelatedIds.Length);
87 success = true;
88 }
89 else
90 {
91 Console.WriteLine("Failed to merge records. Error message: " +
92 mRes.errors[0].message);
93 }
94 }
95 catch (SoapException e)
96 {
97 Console.WriteLine("An unexpected error has occurred: " +
98 e.Message + "\n" + e.StackTrace);
99 }
100 return success;
101}引数
このコールは MergeRequest オブジェクトの配列を受け取ります。MergeRequest オブジェクトには、次のプロパティが含まれます。
| 名前 | 型 | 説明 |
|---|---|---|
| masterRecord | sObject | 必須。その他のレコードのマージ先であるオブジェクトの ID を提供する必要があります。状況に応じて、更新される項目とその値を提供してください。 |
| recordToMergeIds | ID[] | 必須。最小で 1 つ、最大で 2 つ。主レコードにマージされるその他のレコード。 |
| AdditionalInformationMap | map | 項目と値の対応付け。
|