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.
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);
}
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.
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');
}
}
Now, let's try to insert 10 contact out of which 2 will have the email which is already existing in database.
No comments:
Post a Comment