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
CyberfireCyberfire 

Preventing Duplicate Leads

I'm trying to see if I can utilize the cookbook code for the apex trigger on preventing lead duplicates, but have encountered a serious roadblock - if someone submits a web to lead form that is a duplicate and references it in our database, it completely prevents creation of that lead.

For example:

unique lead in database has email of random@google.com

same person with different company submits another web to lead form, with random@google.com - this lead is never created, and we miss out on it.

Any possible workarounds to this? Is there a way to only trigger when a user manually creates a new lead?
aalbertaalbert
The apex trigger will be initiated based on the Object (ie Lead) and the action (ie insert, update, delete).
But within the trigger, you can have logic that only really acts on the data (ie prevent the lead) if specific field values are set per your requirement. For example, if the Lead Source != "Web" , then do apply your de-dupe logic. Otherwise, do nothing and let the Lead be created.

here is some psuedo-code:
Code:
trigger deDupeLeads on Lead (before insert) {


   List<Lead> leadsToProcess = new List<Lead>{};
   for(Lead ld: Trigger.new){
    
       if(ld.Source != 'Web')
             leadsToProcess.add(ld);
   }

   //Now you have all the leads you want to check for duplicates in the List
   //All the others will be processed as is...


}

 

CyberfireCyberfire
So I modified the code, and wondering if someone can polish / troubleshoot with me to get this working:

Code:
trigger leadDuplicatePreventer on Lead
(before insert, before update) {
Map<String, Lead> leadMap = new Map<String, Lead>();
for (Lead lead : System.Trigger.new) {
//Preventing Duplicate Records from Saving
// Make sure we don't treat an email address that
// isn't changing during an update as a duplicate.
if ((lead.Email != null) &&
(System.Trigger.isInsert ||
(lead.Email !=
System.Trigger.oldMap.get(lead.Id).Email && (lead.id).source=='call'))) {
// Make sure another new lead isn't also a duplicate
if (leadMap.containsKey(lead.Email) && (lead.source=='call') {
lead.Email.addError('Another new lead has the '
+ 'same email address.');
} else {
leadMap.put(lead.Email, lead);
}
}
}
// Using a single database query, find all the leads in
// the database that have the same email address as any
// of the leads being inserted or updated.
for (Lead lead : [SELECT Email FROM Lead
WHERE Email IN :leadMap.KeySet()]) {
if ((lead.source=='call')
Lead newLead = leadMap.get(lead.Email));
newLead.Email.addError('A lead with this email '
+ 'address already exists.');
}
}

 

aalbertaalbert
I made some syntax changed to get this trigger to compile.

Code:
trigger leadDuplicatePreventer on Lead(before insert, before update) {
Map<String, Lead> leadMap = new Map<String, Lead>();
for (Lead lead : System.Trigger.new) {
//Preventing Duplicate Records from Saving
// Make sure we don't treat an email address that
// isn't changing during an update as a duplicate.
 if ((lead.Email != null) && (System.Trigger.isInsert || (lead.Email != System.Trigger.oldMap.get(lead.Id).Email && (lead.leadsource=='call'))))  {
 // Make sure another new lead isn't also a duplicate
  if (leadMap.containsKey(lead.Email) && (lead.leadsource=='call')) {
  lead.Email.addError('Another new lead has the ' + 'same email address.');} 
  else {
    leadMap.put(lead.Email, lead);
  }
 }
} 

// Using a single database query, find all the leads in
// the database that have the same email address as any
// of the leads being inserted or updated.
 for (Lead lead : [SELECT Email FROM Lead WHERE Email IN :leadMap.KeySet()]) {
  if (lead.leadsource=='call'){
  
     Lead newLead = leadMap.get(lead.Email);
     newLead.Email.addError('A lead with this email ' + 'address already exists.');
  }
 }
}

 

yespeeyespee
Hi All,

Apologize if I am hijacking this thread. We have similar requirement with slight variation.
We are okay if some company submits a lead with duplicate  email address.
But we don't want to allow a different company to submit a lead with same email which was already submitted.
Can someone help me with this
aalbertaalbert
This should help get you started...it was working in the simplest scenario for me....

Code:
trigger LeadTest on Lead (before insert) {

   List<String> companies = new List<String>{};
   List<String> emails = new List<String>{};
   for(Lead ld: Trigger.new){
    
       if(ld.company!=null)
        companies.add(ld.company);
       
       if(ld.email!=null)
         emails.add(ld.email);
         
   }
   
   
   Lead[] leads = [select id, company, email from lead where company IN :companies OR email IN :emails];
   Map<String,String> leadMap = new Map<String,String>{};
   
   
   for(Lead ld: leads){
       leadMap.put(ld.email,ld.company);
   }
   
   
   for(Lead ld: Trigger.new){

   System.debug('debugging: ' +leadMap.get(ld.email));
   System.debug('debugging: ' +ld.company); 
     if(leadMap.get(ld.email)!=null && leadMap.get(ld.email)!=ld.company){
     ld.addError('add error here'); 
  }
       
   }
}

 

yespeeyespee
Thanks aalbert. I will try this.
yespeeyespee
Any idea why is this not working when a lead is cloned?
aalbertaalbert
Because it only prevents Leads that have the same email address in a different Company. When you clone a record, the company and email will be the same - which in your previous posting , you said was valid.

To verify this, Clone a lead, but modify the Company name before saving. You should see the sample error message "add error here" (which you can modify in the apex code I provided)


yespeeyespee
It is still saving the lead if I clone and change the company name. But works (throws error) when created using 'New' button.