この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

DuplicateResult クラス

重複レコードを検出した重複ルールの詳細と、それらの重複レコードに関する情報を表します。

名前空間

Datacloud

使用方法

重複ルールを使用する組織は、DuplicateResult クラスとそのメソッドを使用できます。

DuplicateResult は、SaveResult の一部である DuplicateError 内に含まれます。SaveResult は、ユーザーが Salesforce にレコードを保存しようとすると生成されます。

次の例は、ユーザーが取引先責任者を追加できるカスタムアプリケーションを示します。取引先責任者が保存されると、重複レコードがある場合はアラートが表示されます。

サンプルアプリケーションは、Visualforce ページと Apex コントローラーで構成されます。Visualforce ページは最初にリストされるため、ページでの Apex コントローラー使用方法を確認できます。Visualforce ページを保存する前に、まず Apex クラスを保存します。

1<apex:page controller="ContactDedupeController">
2    <apex:form >
3        <apex:pageBlock title="Duplicate Records" rendered="{!hasDuplicateResult}">
4            <apex:pageMessages />
5            <apex:pageBlockTable value="{!duplicateRecords}" var="item">
6                <apex:column >
7                    <apex:facet name="header">Name</apex:facet>
8                    <apex:outputLink value="/{!item['Id']}">{!item['Name']}</apex:outputLink>
9                </apex:column>
10                <apex:column >
11                    <apex:facet name="header">Owner</apex:facet>
12                    <apex:outputField value="{!item['OwnerId']}"/>
13                </apex:column>
14                <apex:column >
15                    <apex:facet name="header">Last Modified Date</apex:facet>
16                    <apex:outputField value="{!item['LastModifiedDate']}"/>
17                </apex:column>
18            </apex:pageBlockTable> 
19        </apex:pageBlock>
20        
21        <apex:pageBlock title="Contact" mode="edit">
22            <apex:pageBlockButtons >
23                <apex:commandButton value="Save" action="{!save}"/>
24            </apex:pageBlockButtons>         
25
26            <apex:pageBlockSection >
27                <apex:inputField value="{!Contact.FirstName}"/>
28                <apex:inputField value="{!Contact.LastName}"/>
29                <apex:inputField value="{!Contact.Email}"/>
30                <apex:inputField value="{!Contact.Phone}"/>
31                <apex:inputField value="{!Contact.AccountId}"/>
32            </apex:pageBlockSection>
33        </apex:pageBlock>
34    </apex:form>
35</apex:page>

次のサンプルは、ページの Apex コントローラーです。このコントローラーには、[保存] ボタンのアクションメソッドが含まれます。save メソッドは新しい取引先責任者を挿入します。エラーが返された場合、このメソッドは各エラーを反復処理してそれが重複エラーかどうかをチェックし、ページにエラーメッセージを追加して、重複レコードに関する情報を返してページに表示されるようにします。

1public class ContactDedupeController {
2    
3    // Initialize a variable to hold the contact record you're processing
4    private final Contact contact;
5    
6    // Initialize a list to hold any duplicate records
7    private List<sObject> duplicateRecords;
8    
9    // Define variable that’s true if there are duplicate records
10    public boolean hasDuplicateResult{get;set;}
11    
12    // Define the constructor
13    public ContactDedupeController() {
14        
15        // Define the values for the contact you’re processing based on its ID
16        Id id = ApexPages.currentPage().getParameters().get('id');
17        this.contact = (id == null) ? new Contact() : 
18            [SELECT Id, FirstName, LastName, Email, Phone, AccountId 
19             FROM Contact WHERE Id = :id];
20       
21        // Initialize empty list of potential duplicate records
22        this.duplicateRecords = new List<sObject>();
23        this.hasDuplicateResult = false;
24    }
25    
26    // Return contact and its values to the Visualforce page for display
27    public Contact getContact() {
28        return this.contact;
29    }
30    
31    // Return duplicate records to the Visualforce page for display
32    public List<sObject> getDuplicateRecords() {
33        return this.duplicateRecords;
34    }
35    
36    // Process the saved record and handle any duplicates
37    public PageReference save() {
38        
39        // Optionally, set DML options here, use “DML” instead of “false” 
40        //   in the insert()
41        // Database.DMLOptions dml = new Database.DMLOptions(); 
42        // dml.DuplicateRuleHeader.allowSave = true;
43        // dml.DuplicateRuleHeader.runAsCurrentUser = true;
44        Database.SaveResult saveResult = Database.insert(contact, false);
45
46        if (!saveResult.isSuccess()) {
47            for (Database.Error error : saveResult.getErrors()) {
48                // If there are duplicates, an error occurs
49                // Process only duplicates and not other errors 
50                //   (e.g., validation errors)
51                if (error instanceof Database.DuplicateError) {
52                    // Handle the duplicate error by first casting it as a 
53                    //   DuplicateError class
54                    // This lets you use methods of that class 
55                    //  (e.g., getDuplicateResult())
56                    Database.DuplicateError duplicateError = 
57                            (Database.DuplicateError)error;
58                    Datacloud.DuplicateResult duplicateResult = 
59                            duplicateError.getDuplicateResult();
60                    
61                    // Display duplicate error message as defined in the duplicate rule
62                    ApexPages.Message errorMessage = new ApexPages.Message(
63                            ApexPages.Severity.ERROR, 'Duplicate Error: ' + 
64                            duplicateResult.getErrorMessage());
65                    ApexPages.addMessage(errorMessage);
66                    
67                    // Get duplicate records
68                    this.duplicateRecords = new List<sObject>();
69
70                    // Return only match results of matching rules that 
71                    //  find duplicate records
72                    Datacloud.MatchResult[] matchResults = 
73                            duplicateResult.getMatchResults();
74
75                    // Just grab first match result (which contains the 
76                    //   duplicate record found and other match info)
77                    Datacloud.MatchResult matchResult = matchResults[0];
78
79                    Datacloud.MatchRecord[] matchRecords = matchResult.getMatchRecords();
80
81                    // Add matched record to the duplicate records variable
82                    for (Datacloud.MatchRecord matchRecord : matchRecords) {
83                        System.debug('MatchRecord: ' + matchRecord.getRecord());
84                        this.duplicateRecords.add(matchRecord.getRecord());
85                    }
86                    this.hasDuplicateResult = !this.duplicateRecords.isEmpty();
87                }
88            }
89            
90            //If there’s a duplicate record, stay on the page
91            return null;
92        }
93        
94        //  After save, navigate to the view page:
95        return (new ApexPages.StandardController(contact)).view();
96    }
97    
98}

DuplicateResult のメソッド

DuplicateResult のメソッドは次のとおりです。

getDuplicateRule()

実行されて重複レコードを返した重複ルールの開発者名を返します。

署名

public String getDuplicateRule()

戻り値

型: String

getErrorMessage()

重複レコードを作成している可能性があることをユーザーに警告するために、システム管理者が設定したエラーメッセージを返します。このメッセージは重複ルールに関連付けられています。

署名

public String getErrorMessage()

戻り値

型: String

この例では、新規取引先責任者の保存中に重複が検出されたときに、エラーメッセージを表示するために使用するコードを示します。このコードは、ユーザーが取引先責任者を追加できるカスタムアプリケーションの一部です。取引先責任者が保存されると、重複レコードがある場合はアラートが表示されます。このサンプルアプリケーション全体を確認するには、「DuplicateResult クラス」を参照してください。

1ApexPages.Message errorMessage = new ApexPages.Message(
2                            ApexPages.Severity.ERROR, 'Duplicate Error: ' + 
3                            duplicateResult.getErrorMessage());
4                    ApexPages.addMessage(errorMessage);

getMatchResults()

重複レコードおよび一致情報を返します。

署名

public List<Datacloud.MatchResult> getMatchResults()

戻り値

型: List<Datacloud.MatchResult>

この例では、重複レコードおよび一致情報を返して matchResults 変数に割り当てるために使用するコードを示します。このコードは、ユーザーが取引先責任者を追加するときの重複管理を実装するカスタムアプリケーションの一部です。このサンプルアプリケーション全体を確認するには、「DuplicateResult クラス」を参照してください。

1Datacloud.MatchResult[] matchResults = 
2                            duplicateResult.getMatchResults();

isAllowSave()

重複ルールが、重複と識別されたレコードの保存を許可するかどうかを示します。重複ルールで保存を許可する場合は true、それ以外の場合は false に設定します。

署名

public Boolean isAllowSave()

戻り値

型: Boolean