function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jessica PuckettJessica Puckett 

Update checkbox if specific field on Contact record is a duplicate?

Hello community!  I'm hoping you can give me some direction here...
We have (and allow) multiple Contact records to have the same email address in our Salesforce org.  Our Contacts are our independent agents and often, multiple agents within an agency share the same email address.

I want to find a way for Salesforce to evaluate all the Email values on our Contact records, and flag a (custom field) checkbox if the email is a duplicate. I've been unable to find a way to do this via a Workflow Rule.  Can it be done via an Apex trigger?  I don't have any experience with triggers, so any advice/guidance would be helpful!

Thank you!
Best Answer chosen by Jessica Puckett
Lokesh KumarLokesh Kumar
HI Jessica,

Apologies for the delay.

You can do this pretty easily. For example, if you were creating/updating a contact and wanted to find duplicate email addresses:

Trigger
trigger findDuplicateEmail on Contact(before insert, before update) {
    contactHandler conHan = new contactHandler();
    if(trigger.isInsert && trigger.isBefore){
        conHan.DuplicateChecker(trigger.new);
    }
}

Handler
 
public class contactHandler{
    set<string> setEmails = new set<string>();
    list<contact> lstContact = new list<contact>();
    map<string, contact> mapEmailWithContact = new map<string, contact>();


    public void DuplicateChecker(List<contact> lstNewContact){
        for(contact oContact : lstNewContact){
            if(oContact.email != null){
             setEmails.add(oContact.email);
            }
        }
        lstContact = [select id, phone, email from contact where email in :setEmails ];
        for(Contact oContact : lstContact){
            if(string.isNotEmpty(oContact.email)){
                mapEmailWithContact.put(oContact.email, oContact);
            }
        }

        for(contact oContact : lstNewContact){
            if(mapEmailWithContact.containsKey(oContact.email)){
                oContact.duplicate__c = true;
            }
        }


    }

}

 

All Answers

Amit Singh 1Amit Singh 1
Hello Jessica,

Correct me if I am wrong.

You want to update custom checkbox in Contact field if same email has been attached to many contacts for existing contacts?
If this is the case then you need to write a batch which will be on Contact Object and responsible for quering all the contact and the updating the contact based on the email.

If you want to update the field for the newly created rocord then you can write the trigger on the Contact Object which will check if the contact Object record alredy exists then update the checkbox.

Let me know if this helps :)

Thanks!
AMit SIngh
Jessica PuckettJessica Puckett
Hi, Amit,
You are correct, I would like for this field update to occur on existing Contact records with duplicate email addresses as well as newly created Contacts going forward.  Your explanation makes sense to me, unfortunately, my lack of experience with apex is limiting my ability to write a correct batch/trigger.  Can you suggest any materials/examples I could reference in regard to writing them?
Thank you!
Lokesh KumarLokesh Kumar
Hi Jessica,

It can be implemented in a trigger, yes. But you should rather consider the Manage Duplicate Records (https://help.salesforce.com/articleView?id=managing_duplicates_overview.htm&language=en_US&type=0) in Salesforce feature. Note that the Allow option includes the ability to report on the duplicate records.

User-added image

Hope it will solve your problem please mark this as a Best answer.
Jessica PuckettJessica Puckett
Hello Lorkesh,
Thanks for your response, however I don't see a way to update a separate field value based on the Matching/Duplicate Rules.  Am I missing something? 
Thank you!
Lokesh KumarLokesh Kumar
Ok, why do you think you need to populate a checkbox? 
Jessica PuckettJessica Puckett
It all comes down to Pardot, actually.  We have enabled Multiple Prospects with the Same Email Address within Pardot.  Our Contact records in Salesforce are created as Prospects in Pardot.  In order to "save space" when it comes to our Prospect limit, I have some Automation Rules built which cause a Prospect/Contact to be mailable (or not) based on Pardot Custom Fields (which are fed directly from Salesforce fields). 

Currently, if I have 5 Contacts within an agency using the same email address, and only one of those Contacts meets my "unmailalble" criteria, it will cause the email address to be flagged "Do Not Email", preventing the other four agents from receiving the email.

I need an "email duplicate" checkbox which I can add as a Custom Field in Pardot and subsequently edit my Automation Rule criteria to say IF "email duplicate"=true THEN do not mark unmailable.
Lokesh KumarLokesh Kumar
HI Jessica,

Apologies for the delay.

You can do this pretty easily. For example, if you were creating/updating a contact and wanted to find duplicate email addresses:

Trigger
trigger findDuplicateEmail on Contact(before insert, before update) {
    contactHandler conHan = new contactHandler();
    if(trigger.isInsert && trigger.isBefore){
        conHan.DuplicateChecker(trigger.new);
    }
}

Handler
 
public class contactHandler{
    set<string> setEmails = new set<string>();
    list<contact> lstContact = new list<contact>();
    map<string, contact> mapEmailWithContact = new map<string, contact>();


    public void DuplicateChecker(List<contact> lstNewContact){
        for(contact oContact : lstNewContact){
            if(oContact.email != null){
             setEmails.add(oContact.email);
            }
        }
        lstContact = [select id, phone, email from contact where email in :setEmails ];
        for(Contact oContact : lstContact){
            if(string.isNotEmpty(oContact.email)){
                mapEmailWithContact.put(oContact.email, oContact);
            }
        }

        for(contact oContact : lstNewContact){
            if(mapEmailWithContact.containsKey(oContact.email)){
                oContact.duplicate__c = true;
            }
        }


    }

}

 
This was selected as the best answer