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
jsacpt24jsacpt24 

Lead dedupe

I wrote a dedupe trigger that is supposed to check to see if the lead already exists in the system by email and contact. If the contact already exist then I want it to add the original lead to a lookup field. If it is not a duplicate then it would mark is_primary__c. I don't see it updating when I go and create a new lead manually. Is there something in my code that is causing it to not work? 
 
trigger findLeadDupes on Lead (before insert) {
  for (Lead myLead : Trigger.new) {
    if (myLead.Email != null) {
      List<Lead> dupes = [SELECT Id FROM Lead
                               WHERE Email = :myLead.Email AND Company = :myLead.Company];
      if (dupes.size() > 0) {
        myLead.Primary_Lead__c = dupes[0].Id;
      } else {
        myLead.Primary_Lead__c = null;
        myLead.Is_Primary__c = True;
      }                             
    }
  }
}

 
Best Answer chosen by jsacpt24
PRAKASH JADA 13PRAKASH JADA 13
Hi,

In the above code, you are using before Insert. For before Insert the values that you are entering are not committed to database so it will show you null value.
try the below script

Trigger:
-----------------
trigger findLeadDupes on Lead (after insert) {
    if(Trigger.isAfter) {
        if(Trigger.isInsert) {
            findLeadDupesHandler.onAfterInsert(Trigger.New);
        }
           
    }
}


Handler:
-----------------------
public class findLeadDupesHandler {
    // Method to execute on After Insert of lead
    public static void onAfterInsert(List<Lead> leads) {
        Map<String, Id> leadMap = new Map<String, Id>();
        List<Lead> updateList     = new List<Lead>();
        
        // Lead to get all the lead records based on created Date
        List<Lead> leadList = [SELECT Id, Email, Company FROM Lead Order By CreatedDate DESC];
        
        // Loop to iterate over the List of Lead result records
        for(Lead lead : leadList) {
            string concatinatedValue = lead.Email + ' : ' + lead.Company;
            leadMap.put(concatinatedValue, Lead.Id);
        }
        
        // Loop to iterate over the list of records
        for(Lead lead : leads) {
            // Condition to check for the concated string
            if(leadMap.containsKey(lead.Email + ' : ' +lead.Company)) {
                Lead updateLead = new Lead(Id = lead.Id ,Primary_Lead__c  = leadMap.get(lead.Email + ' : ' +lead.Company) );
                updateList.add(updateLead);
            } else {
                Lead updateLead = new Lead(Id = lead.Id ,Is_Primary__c  = true);
                updateList.add(updateLead);
            }
        }
        
        try{
            update updateList;
        } catch(DMLException e) {
            System.debug('Unable to update the Lead Record : ' + e.getMessage());
        }
        
    }
}

All Answers

PRAKASH JADA 13PRAKASH JADA 13
Hi,

In the above code, you are using before Insert. For before Insert the values that you are entering are not committed to database so it will show you null value.
try the below script

Trigger:
-----------------
trigger findLeadDupes on Lead (after insert) {
    if(Trigger.isAfter) {
        if(Trigger.isInsert) {
            findLeadDupesHandler.onAfterInsert(Trigger.New);
        }
           
    }
}


Handler:
-----------------------
public class findLeadDupesHandler {
    // Method to execute on After Insert of lead
    public static void onAfterInsert(List<Lead> leads) {
        Map<String, Id> leadMap = new Map<String, Id>();
        List<Lead> updateList     = new List<Lead>();
        
        // Lead to get all the lead records based on created Date
        List<Lead> leadList = [SELECT Id, Email, Company FROM Lead Order By CreatedDate DESC];
        
        // Loop to iterate over the List of Lead result records
        for(Lead lead : leadList) {
            string concatinatedValue = lead.Email + ' : ' + lead.Company;
            leadMap.put(concatinatedValue, Lead.Id);
        }
        
        // Loop to iterate over the list of records
        for(Lead lead : leads) {
            // Condition to check for the concated string
            if(leadMap.containsKey(lead.Email + ' : ' +lead.Company)) {
                Lead updateLead = new Lead(Id = lead.Id ,Primary_Lead__c  = leadMap.get(lead.Email + ' : ' +lead.Company) );
                updateList.add(updateLead);
            } else {
                Lead updateLead = new Lead(Id = lead.Id ,Is_Primary__c  = true);
                updateList.add(updateLead);
            }
        }
        
        try{
            update updateList;
        } catch(DMLException e) {
            System.debug('Unable to update the Lead Record : ' + e.getMessage());
        }
        
    }
}
This was selected as the best answer
jsacpt24jsacpt24
Hello Prakash, 

it looks like it is saving properly but I am not seeing the duplicates be added to the field or have the is primary boolean checked. Is there something you recommend I do to tweak or check for this? 
PRAKASH JADA 13PRAKASH JADA 13
Hi,

After saving the code you can go and clone the lead record. In that way, you can validate that the code is working or not. Create a new lead with new email and company name the primary flag should be populated.
 
jsacpt24jsacpt24
It wasn't working but when I put the is_primary__c = true for the where clause of the SOQL it worked. Looks like prior to that it was trying to update the Primary_Lead with itself and causing an error. I appreciate the help!