Saturday, 8 April 2017

Displaying Duplicate Error and Matching Records on VF Page:

Before going to understand the below code you must have to create a duplicate rule for object which you want to upsert(insert / update) from Visualforce page action button(command button). So to achieve this you have to create a duplicate rule on your Dev Org / SB Org and then create a Visualforce page with custom controller. For e.g.
VF Page Name- MatchingRuleDataDisplay
Class Name- clsMatchingRuleDataDisplay

Custom Controller Apex Code Method in Salesforce

//Adding only method to minimize the code in this post
public void insertUsageData()
{
    //Variable is defined at class level
    hasDuplicateResult = false;
    intUsageDataCount = 0;
  
    //Variable(Property) is defined at class level
    if(lstUsageToInsert.size() > 0)
    {
        //Variable(Property) is defined at class level
        duplicateRecords = new List<sObject>();
      
        // DML statement and perpare your data with your required SObject that has duplicate rule
        Database.SaveResult[] srList = Database.insert(lstUsageToInsert, false);
      
        for (Database.SaveResult sr : srList)
        {
            if (!sr.isSuccess())
            {
                for (Database.Error error : sr.getErrors())
                {
                    if (error instanceof Database.DuplicateError)
                    {
                        Database.DuplicateError duplicateError =  (Database.DuplicateError)error;
                        Datacloud.DuplicateResult duplicateResult = duplicateError.getDuplicateResult();
                      
                        // Display duplicate error message as defined in the duplicate rule
                        ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.Severity.ERROR, 'Duplicate Error: ' +  duplicateResult.getErrorMessage());
                        ApexPages.addMessage(errorMessage);
                      
                        for(Datacloud.MatchResult matchResult : duplicateResult.getMatchResults())
                        {
                            // Add matched record to the duplicate records variable
                            for (Datacloud.MatchRecord matchRecord : matchResult.getMatchRecords())
                            {
                                System.debug('MatchRecord: ' + matchRecord.getRecord());
                                duplicateRecords.add(matchRecord.getRecord());
                            }
                        }
                      
                        hasDuplicateResult = !duplicateRecords.isEmpty();
                    }
                }
            }
        }
      
        if(!hasDuplicateResult)
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Confirm,'Process has been completed successfully.'));
    }
}

Visualforce Page Code

<apex:pageBlock title="Usage Duplicate Records" rendered="{!hasDuplicateResult}">
    <apex:pageBlockTable value="{!duplicateRecords}" var="item">
        <apex:column >
            <apex:facet name="header">Name</apex:facet>
            <apex:outputLink value="/{!item['Id']}" target="_blank">{!item['Name']}</apex:outputLink>
        </apex:column>
        <apex:column >
            <apex:facet name="header">Owner</apex:facet>
            <apex:outputField value="{!item['OwnerId']}"/>
        </apex:column>
        <apex:column >
            <apex:facet name="header">Last Modified Date</apex:facet>
            <apex:outputField value="{!item['LastModifiedDate']}"/>
        </apex:column>
    </apex:pageBlockTable>
</apex:pageBlock>
....AND HERE WE ARE WITH FINAL OUTPUT.

No comments:

Post a Comment