0

I know everyone here is big on bulkification..but I've never done it myself. Could somebody give me some pointers on how to bulkify this Trigger? It is a basic duplicate lead trigger.

//Look for duplicate leads based on email
//Populate a Dupe_Lead__c field with the Lead if found

trigger DetectDupes on Lead (before insert, before update)
{
    for (Lead l : Trigger.new)
    //Make sure the lead has an email address
    {
        if (l.Email != null)
        {
            //Find a duplicate
            String leadEmail = l.Email;
            String leadId = l.Id; 
            Date d = system.today().addDays(-7);

            List<Lead> dupeLeads = [SELECT Id FROM Lead WHERE Email = :leadEmail AND Id != :leadId AND createdDate >= :d AND Email != 'na@na.na' AND Email != 'na@na.ca' AND Email != 'na@na.com'];

            //If a duplicate is found, update the Dupe_Lead__c field. There is a validation rule that will not allow the record to save if this field is populated.
            if (dupeLeads.size() > 0)
            {
                l.Dupe_Lead__c = dupeLeads[0].Id;
            }
            else
            {
                l.Dupe_Lead__c = null;
            }
        }    
        else 
        {
             l.Dupe_Lead__c = null;   
        }

    }
}
1

The key is to gather your identifiers, query for existing records and place them in a collection, the reprocess the trigger records checking the collection for the duplicate data:

(Sets are Case Sensitive so keep that in mind, you may want to convert the email to lowercase before adding)

Step 1 - Gather identifiers

Set<String> emailsToCheck = New Set<String>();
Set<String> emailsInTrigger = New Set<String>();

for(Lead l : trigger.new){
    if(l.email != null){
      emailsToCheck.add(l.email);
      if(emailsInTrigger.contains(l.email){
          l.addError('Duplicate Lead in trigger');
      else
          emailsInTrigger.add(l.email);
    }
}

Step 2 - Query for Records and store duplicate emails in a collection Set emailsFound = New Set();

for(Lead l : [SELECT Id FROM Lead WHERE Email IN :emailsToCheck AND createdDate >= :d AND Email != 'na@na.na' AND Email != 'na@na.ca' AND Email != 'na@na.com']){
     emailsFound.add(l.email);
}

NOTE: This is a non-selective query and could cause issues

Step 3 - Process records

for(lead l : trigger.new){
   if(l.email != null && emailsFound.contains(l.email))
        l.addError('Lead is a duplicate);
}

It also removes the requirement for the workflow rule...

An easier way to do it would simply to create a unique Index field on the lead that a workflow updates to the value of the email and set it to unique. Problem solved

Here is an answer with a lot more detail on this subject:

General trigger bulkification - best practices

|improve this answer|||||
  • Thanks for the thorough breakdown! This should help me a lot with this trigger and future ones! – Alex Brigham Aug 18 '15 at 19:16

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