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>

My Apex code:

..to be added ..
  • Have you tried using trigger?
    – snehakem
    May 19, 2015 at 6:24
  • no, i am entering value from VF page.
    – V T
    May 19, 2015 at 6:31
  • @VT can you please elaborate your problem...? for example which object record are you creating and criteria to check duplicates ? Also, please post your VF page code.
    – Yogesh D
    May 19, 2015 at 6:32
  • I have edited my question.
    – V T
    May 19, 2015 at 6:39
  • @VT okay... it is more clear now....but one more thing is when should the duplicate checker be called before pressing the 'Create User' button or after pressing 'Create User'?
    – Yogesh D
    May 19, 2015 at 6:46

1 Answer 1


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 = 
                Datacloud.DuplicateResult duplicateResult = 

                // Display duplicate error message as defined in the duplicate rule
                ApexPages.Message errorMessage = new ApexPages.Message(
                        ApexPages.Severity.ERROR, 'Duplicate Error: ' + 

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

                // Return only match results of matching rules that 
                //  find duplicate records
                Datacloud.MatchResult[] matchResults = 

                // 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.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();



You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .