[SalesForce] Checking Duplicates values after entering value

The problem is here how do i check duplicate values after entering values.
When user enters Userid in Userid field then the System checks if there is already a user in the system based on the id mentioned in the userid field on the account record.

  1. User opens the broker account for which a user needs to be created in the system.
  2. User clicks on ‘Create user’ button on top of the page. This button is not activated till the Account is onboarded.
  3. System checks if there is already a user in the system based on the id mentioned in the userid field on the account record.
  4. If there is no value in this field, the system will complete this duplicity check on the basis of email id of the Account.
  5. If no matching user is found, system will create a user in the system using the Account information.
  6. The userid field from the Account will be the userid of the user.

My Visualforce code:

<apex:page controller="createuser" action="{!fetchAccount}">
     <apex:pageMessages ></apex:pageMessages>
</apex:page>

My Apex code:

..to be added ..

Best Answer

This is the answer.........

public class ContactDedupeController {

// Initialize a variable to hold the contact record you're processing
private final Contact contact;

// Initialize a list to hold any duplicate records
private List<sObject> duplicateRecords;

// Define variable that’s true if there are duplicate records
public boolean hasDuplicateResult{get;set;}

// Define the constructor
public ContactDedupeController() {

    // Define the values for the contact you’re processing based on its ID
    Id id = ApexPages.currentPage().getParameters().get('id');
    this.contact = (id == null) ? new Contact() : 
        [SELECT Id, FirstName, LastName, Email, Phone, AccountId 
         FROM Contact WHERE Id = :id];

    // Initialize empty list of potential duplicate records
    this.duplicateRecords = new List<sObject>();
    this.hasDuplicateResult = false;
}

// Return contact and its values to the Visualforce page for display
public Contact getContact() {
    return this.contact;
}

// Return duplicate records to the Visualforce page for display
public List<sObject> getDuplicateRecords() {
    return this.duplicateRecords;
}

// Process the saved record and handle any duplicates
public PageReference save() {

    // Optionally, set DML options here, use “DML” instead of “false” 
    //   in the insert()
    // Database.DMLOptions dml = new Database.DMLOptions(); 
    // dml.DuplicateRuleHeader.allowSave = true;
    // dml.DuplicateRuleHeader.includeRecordDetails = true;
    // dml.DuplicateRuleHeader.runsAsCurrentUser = true;
    Database.SaveResult saveResult = Database.insert(contact, false);

    if (!saveResult.isSuccess()) {
        for (Database.Error error : saveResult.getErrors()) {
            // If there are duplicates, an error occurs
            // Process only duplicates and not other errors 
            //   (e.g., validation errors)
            if (error instanceof Database.DuplicateError) {
                // Handle the duplicate error by first casting it as a 
                //   DuplicateError class
                // This lets you use methods of that class 
                //  (e.g., getDuplicateResult())
                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);

                // Get duplicate records
                this.duplicateRecords = new List<sObject>();

                // Return only match results of matching rules that 
                //  find duplicate records
                Datacloud.MatchResult[] matchResults = 
                        duplicateResult.getMatchResults();

                // Just grab first match result (which contains the 
                //   duplicate record found and other match info)
                Datacloud.MatchResult matchResult = matchResults[0];

                Datacloud.MatchRecord[] matchRecords = matchResult.getMatchRecords();

                // Add matched record to the duplicate records variable
                for (Datacloud.MatchRecord matchRecord : matchRecords) {
                    System.debug('MatchRecord: ' + matchRecord.getRecord());
                    this.duplicateRecords.add(matchRecord.getRecord());
                }
                this.hasDuplicateResult = !this.duplicateRecords.isEmpty();
            }
        }

        //If there’s a duplicate record, stay on the page
        return null;
    }

    //  After save, navigate to the view page:
    return (new ApexPages.StandardController(contact)).view();
}

}

Related Topic