Saturday, October 29, 2022

Apex trigger to avoid insertion of duplicate contact in salesforce

 In this blog we will learn how to prevent insertion of duplicate contact in salesforce when a contact already exist in salesforce with same email.

TriggerHandler class.

An interface is a class where we create method signatures but body of the method is not
defined.

public interface TriggerHandler {

    void beforeInsert(List<SObject> newRecordsList);

    void beforeUpdate(List<SObject> oldRecordsList, List<SObject> newRecordsList, Map<ID, SObject> oldRecordsMap, Map<ID, SObject> newRecordsMap);

    void beforeDelete(List<SObject> oldRecordsList, Map<ID, SObject> oldRecordsMap);

    void afterDelete(List<SObject> oldRecordsList, Map<ID, SObject> oldRecordsMap);

    void afterInsert(List<SObject> newRecordsList, Map<ID, SObject> newRecordsMap);

    void afterUpdate(List<SObject> oldRecordsList, List<SObject> newRecordsList, Map<ID, SObject> oldRecordsMap, Map<ID, SObject> newRecordsMap);

    void afterUndelete(List<SObject> newRecordsList, Map<ID, SObject> newRecordsMap);

}


The handler class will implement the above interface.

Trigger Handler Class:

public without sharing class ContactTriggerHandler implements TriggerHandler {

   ContactTriggerHelper helperClass = new ContactTriggerHelper();

    public void beforeInsert(List<Contact> newConList) {

        helperClass.methodToHandleBeforeInsert(newConList);

    }

    public void beforeUpdate(List<Contact> oldConList, List<Contact> newConList, Map<ID, SObject> oldConMap, Map<ID, SObject> newConMap) {

         helperClass.methodToHandleBeforeUpdate();

    }

    public void beforeDelete(List<Contact> oldConList, Map<ID, SObject> oldConMap) {

        helperClass.methodToHandleBeforeDelete();

    }

    public void afterDelete(List<Contact> oldConList, Map<ID, SObject> oldConMap) {

        helperClass.methodToHandleAfterDelete();

    }

    public void afterInsert(List<Contact> newConList, Map<ID, SObject> newConMap) {

        helperClass.methodToHandleAfterInsert();

    }

    public void afterUpdate(List<Contact> oldConList, List<Contact> newConList, Map<ID, SObject> oldConMap, Map<ID, SObject> newConMap) {

      helperClass.methodToHandleAfterUpdate();

    }

    public void afterUndelete(List<Contact> newConList, Map<ID, SObject> newConMap) {

      helperClass.methodToHandleAfterUndelete();

    }

}

Now, Let's define our helper class.

Trigger Helper Class:

public without sharing class ContactTriggerHelper {

    public void methodToHandleBeforeInsert(List<Contact> newConList) {

        System.debug('Inside methodToHandleBeforeInsert');

        Set<string> emailOfIncomingContacts = new Set<String>();

        // Storing incoming contact email in set of string

        Set<string> existingConCheckFromDatabaseWithIncomingEmail = new Set<string>();

        for(Contact con:newConList){

        emailOfIncomingContacts.add(con.email);

        }

        // Querying contact from database to check if any contact exist with incoming email

        for(Contact con2:[SELECT id,email from Contact where email in:emailOfIncomingContacts]){

        existingConCheckFromDatabaseWithIncomingEmail.add(con2.email);

        }

             // Preventing duplicate contact from getting inserted in database.

        for(Contact con3:newConList){

           if(existingConCheckFromDatabaseWithIncomingEmail.contains(con3.email)){

           con3.addError('Contact already exists in database with email'+con3.email);

           }

        }

    }

    public void methodToHandleBeforeUpdate() {

        System.debug('Inside methodToHandleBeforeUpdate');

    }

    public void methodToHandleBeforeDelete() {

        System.debug('Inside methodToHandleBeforeDelete');

    }

    public void methodToHandleAfterDelete() {

        System.debug('Inside methodToHandleAfterDelete');

    }

    public void methodToHandleAfterInsert() {

 

        System.debug('Inside methodToHandleAfterInsert');

    }

    public void methodToHandleAfterUpdate() {

        System.debug('Inside methodToHandleAfterUpdate');

    }

    public void methodToHandleAfterUndelete() {

        System.debug('Inside methodToHandleAfterUndelete');

    }

}


Below is the list of contact in my database,

Apex trigger to avoid duplicate contact in salesforce

Now, let's try to insert 10 contact out of which 2 will have the email which is already existing in database.

CSV file is shown below.

Trigger to avoid duplicates in Salesforce


Now, let's try to insert the above 1o contact list from workbench and see the result as shown below.

Apex trigger to avoid duplicate contact in salesforce

No comments:

Post a Comment