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!

More detail

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.


You should read up on The XY Problem. You are really fixated on how to implement a specific approach to get to your end goal, instead of just looking at the end goal and implementing the simplest approach to that problem.

Duplicate checks can be implemented in a trigger, yes. But you should rather consider the Manage Duplicate Records in Salesforce feature. Note that the Allow option includes the ability to report on the duplicate records.

Here’s how duplicate management works.

  • When a user tries to save a new record, the record is first compared with existing Salesforce records to identify possible duplicates (1). The criteria used to compare records and identify the possible duplicates are defined by a matching rule. Next, a list of possible duplicates is returned (2). What happens when the record being saved is identified as a possible duplicate depends on what’s defined in the duplicate rule (3). For example, the duplicate rule could block users from saving the possible duplicate record or allow them to save it anyway. Both the Block and Allow options include an alert, which tells users why they can’t save the record and what to do about it. The Allow option includes the ability to report on the duplicate records.

  • When a user tries to save an edited record, the record is checked to see if the user has changed the value of a matching rule field. If so, the duplicate management process works as described for new records. If not, no further action is taken and duplicates are not detected.

Duplicate prevention flow


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


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


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){
        lstContact = [select id, phone, email from contact where email in :setEmails ];
        for(Contact oContact : lstContact){
                mapEmailWithContact.put(oContact.email, oContact);

        for(contact oContact : lstNewContact){
                oContact.duplicate__c = true;


  • QUERY inside a for loop in a trigger!?? – Raul Feb 23 '17 at 12:41
  • My Bad! Updated the answer – Samir Feb 23 '17 at 13:13
  • Great, but what if I upload records in dataloader and they are duplicates. Your code might not work for it. :) – Raul Feb 23 '17 at 14:16
  • That we can manually set to true in the excel itself before uploading the file through data loader :) – Samir Feb 23 '17 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.