レコードのマージ
データベースのリード、取引先責任者、ケース、または取引先レコードが重複している場合、データをクリーンアップしてレコードを統合することをお勧めします。同じ sObject 型のレコードを 3 つまでマージできます。merge 操作は、最大 3 つのレコードを 1 つのレコードにマージし、他のレコードを削除してから、関連レコードを再ペアレント化します。
例
次に、既存の取引先レコードを主取引先にマージする方法を示します。マージする取引先には、マージ操作後に主取引先レコードに移動される関連取引先責任者があります。また、マージするレコードはマージ後に削除され、1 つのレコードのみがデータベースに残ります。この例では、2 つの取引先のリストを作成してからリストを挿入します。次に、クエリを実行して新規取引先レコードをデータベースから取得し、マージする取引先に取引先責任者を追加します。その後、2 つの取引先をマージします。最後に、取引先責任者が主取引先に移動していて、2 つ目の取引先が削除されていることを確認します。
1// Insert new accounts
2List<Account> ls = new List<Account>{
3 new Account(name='Acme Inc.'),
4 new Account(name='Acme')
5 };
6insert ls;
7
8// Queries to get the inserted accounts
9Account masterAcct = [SELECT Id, Name FROM Account WHERE Name = 'Acme Inc.' LIMIT 1];
10Account mergeAcct = [SELECT Id, Name FROM Account WHERE Name = 'Acme' LIMIT 1];
11
12// Add a contact to the account to be merged
13Contact c = new Contact(FirstName='Joe',LastName='Merged');
14c.AccountId = mergeAcct.Id;
15insert c;
16
17try {
18 merge masterAcct mergeAcct;
19} catch (DmlException e) {
20 // Process exception
21 System.debug('An unexpected error has occurred: ' + e.getMessage());
22}
23
24// Once the account is merged with the master account,
25// the related contact should be moved to the master record.
26masterAcct = [SELECT Id, Name, (SELECT FirstName,LastName From Contacts)
27 FROM Account WHERE Name = 'Acme Inc.' LIMIT 1];
28System.assert(masterAcct.getSObjects('Contacts').size() > 0);
29System.assertEquals('Joe', masterAcct.getSObjects('Contacts')[0].get('FirstName'));
30System.assertEquals('Merged', masterAcct.getSObjects('Contacts')[0].get('LastName'));
31
32// Verify that the merge record got deleted
33Account[] result = [SELECT Id, Name FROM Account WHERE Id=:mergeAcct.Id];
34System.assertEquals(0, result.size());この 2 番目の例は前の例と同様ですが、Database.merge メソッド (merge ステートメントではない) を使用する点が異なります。Database.merge の最後の引数は false に設定され、この操作で発生したすべてのエラーが、例外を使用するのではなく、マージ結果で返されるようにします。この例では、2 つの取引先を主取引先にマージし、返された結果を取得します。また、この例では、主取引先と 2 つの複製が作成され、それぞれが 1 つの子取引先責任者を持ちます。これにより、マージ後に取引先責任者が主取引先に移動されることが検証されます。
1// Create master account
2Account master = new Account(Name='Account1');
3insert master;
4
5// Create duplicate accounts
6Account[] duplicates = new Account[]{
7 // Duplicate account
8 new Account(Name='Account1, Inc.'),
9 // Second duplicate account
10 new Account(Name='Account 1')
11};
12insert duplicates;
13
14// Create child contact and associate it with first account
15Contact c = new Contact(firstname='Joe',lastname='Smith', accountId=duplicates[0].Id);
16insert c;
17
18
19
20// Get the account contact relation ID, which is created when a contact is created on "Account1, Inc."
21AccountContactRelation resultAcrel = [SELECT Id FROM AccountContactRelation WHERE ContactId=:c.Id LIMIT 1];
22
23
24// Merge accounts into master
25Database.MergeResult[] results = Database.merge(master, duplicates, false);
26
27for(Database.MergeResult res : results) {
28 if (res.isSuccess()) {
29 // Get the master ID from the result and validate it
30 System.debug('Master record ID: ' + res.getId());
31 System.assertEquals(master.Id, res.getId());
32
33 // Get the IDs of the merged records and display them
34 List<Id> mergedIds = res.getMergedRecordIds();
35 System.debug('IDs of merged records: ' + mergedIds);
36
37 // Get the ID of the reparented record and
38 // validate that this the contact ID.
39 System.debug('Reparented record ID: ' + res.getUpdatedRelatedIds());
40
41 // Make sure there are two IDs (contact ID and account contact relation ID); the order isn't defined
42 System.assertEquals(2, res.getUpdatedRelatedIds().size() );
43 boolean flag1 = false;
44 boolean flag2 = false;
45
46
47 // Because the order of the IDs isn't defined, the ID can be at index 0 or 1 of the array
48 if (resultAcrel.id == res.getUpdatedRelatedIds()[0] || resultAcrel.id == res.getUpdatedRelatedIds()[1] )
49 flag1 = true;
50
51 if (c.id == res.getUpdatedRelatedIds()[0] || c.id == res.getUpdatedRelatedIds()[1] )
52 flag2 = true;
53
54 System.assertEquals(flag1, true);
55 System.assertEquals(flag2, true);
56
57 }
58 else {
59 for(Database.Error err : res.getErrors()) {
60 // Write each error to the debug output
61 System.debug(err.getMessage());
62 }
63 }
64}マージに関する考慮事項
- リード、取引先責任者、ケース、および取引先のみがマージ可能です。「DML 操作をサポートしない sObject」を参照してください。
- 1 つの merge メソッドには、1 つの主レコードと最大 2 つのその他の sObject レコードを渡すことができます。
- Apex マージ操作を使用すると、主レコードの項目値の方が、マージされたレコードの対応する項目値よりも常に優先されます。マージされたレコード項目値を維持するには、マージを実行する前に、この項目値を単に主 sObject に設定します。
- 外部 ID 項目では、merge を使用することはできません。
リード、取引先責任者、および取引先のマージについての詳細は、Salesforce オンラインヘルプを参照してください。