Salesforce Apex Trigger to Prevent Duplicate Contacts - For Beginners

Trigger to Prevent Duplicate Contacts Using Email or Phone Number

Salesforce Apex Trigger to Prevent the user from creating Duplicate Contacts when a Contact already exists with the same Email or Phone number – Triggers for Beginners

The requirement is:

When a Contact is being created, if a record with the same Email or Phone number exists, the trigger should throw an error and the user should not be allowed to create the duplicate Contact.

To achieve this, we will write a trigger with the following flow:

  1. Define the Trigger
  2. Create Sets to store the ‘Email Ids’ and ‘Phone Numbers’ of all the records that are being created
  3. Iterate through each record that is being created, and add their ‘Email Ids’ and ‘Phone Numbers’ to the Sets.
  4. Create a List to store the ‘Email Ids’ and ‘Phone Numbers’ of existing records that match the records being created.
  5. Populate the above List with of existing records whose email id or phone number matches the ones being created.
  6. Iterate through each record being created and display and error if the record already exists.

Now lets go through each step once again and write its respective code.

1. Defining the trigger (the trigger syntax)

Lets name the trigger as “PreventDuplicateContacts“. The object name would be “Contact” and the trigger event would be “before insert” as we have to check if a duplicate record exists and display the error before the Contact is created.

trigger PreventDuplicateContacts on Contact (before insert) {

}

 

2. Create Sets

In this step, we would be creating two Sets of data type String to hold Email ids and Phone numbers of records that are being created. Lets call these Sets emailSet and phoneSet respectively

Set <String> emailSet = new Set<String>();
Set <String> phoneSet = new Set<String>();

3. Iterate through records being created

Here we will iterate through each Contact record that is being created. During iteration, we will add the email id and phone number of each Contact  to their Sets.

for (contact con:trigger.new) {
     emailSet.add(con.email);
     phoneSet.add(con.phone);
}

 

4. Create a List

We will now create a blank List named contactList. This List will be used in the next step to hold Contact Id of existing Contacts.

// New list to store the found email or phone numbers
List <Contact> contactList = new List<Contact>();

 

5. Populate the list with existing records

Using SOQL. the above List is now being populated with the Id of existing records, whose “Email or Phone number” matches the “Email or Phone number” of the records being created. As the Email and Phone of the records being created was stored in the Sets in Step 2, those sets are being used as reference in SOQL.

contactList = [SELECT email,phone FROM Contact WHERE email IN :emailSet OR phone IN :phoneSet];

 

6. Iterate through Records and Display Error.

Now iterate through each record that is being created and display an Error on the Email field. If the Size of the list is greater than zero, it will mean that the existing record was found.

for (contact con:trigger.new) {
     If(contactList.size() > 0) {
     con.email.adderror( 'Duplicate Contact Found. Use Existing Contact.' ); // Displaying the error
     }
}

 

The complete trigger to prevent duplicate contacts by matching email or phone number and displaying the error on the email field would be:

trigger PreventDuplicateContacts on Contact (before insert) {
    
    // Set to store email ids
    Set <String> emailSet = new Set<String>(); 
    // Set to store phone numbers
    Set <String> phoneSet = new Set<String>(); 
    
    // Iterate through each Contact and add their email and phone number to their respective Sets
    for (contact con:trigger.new) {
        emailSet.add(con.email);
        phoneSet.add(con.phone);
    }

    // New list to store the found email or phone numbers
    List <Contact> contactList = new List<Contact>();

    // Populating the list using SOQL
    contactlist = [SELECT email,phone FROM Contact WHERE email IN :emailSet OR phone IN :phoneSet];

    // Iterating through each Contact record to see if the same email or phone was found
    for (contact con:trigger.new) {
        If (contactList.size() > 0) {
            // Displaying the error
            con.email.adderror( 'Duplicate Contact Found. Use Existing Contact.' );
        }
    }

}

 

Here is how the Trigger to Prevent Duplicate Contacts looks in the Salesforce Developer Console (image)

Salesforce Apex Trigger to Prevent Duplicate Contacts - For Beginners
Salesforce Apex Trigger to Prevent Duplicate Contacts – For Beginners

 

 


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.