3

I have a trigger on lead which checks for duplicate leads on before insert and then assigns the duplicate lead field with the existing lead from the database.But the issue if there duplicates from the records being inserted not from the database.I need to insert all the dupes.One of the record is not duplicate and rest of the them assign to first record.Can someone point me how to handle this case.

trigger LeadDupeAssociation on Lead(before insert){

Map<String,Lead> PhoneAndExistingLeadMap = new Map<String,Lead>();
Map<String,Lead> EmailAndExistingLeadMap = new Map<String,Lead>();
Map<String,Lead> nameLocationAndExistingLeadMap = new Map<String,Lead>();


    phoneIdSet = new Set<String>();
    emailIdSet = new Set<String>();
    locationSet = new Set<String>();

    firstNameSet = new Set<String>();
    lastNameSet = new Set<String>();
    citySet = new Set<String>();
    stateSet = new Set<String>();

    Set<Id> newLeadOwnerIdSet = new Set<Id>();
    Set<Id> existingAccIdSet = new Set<Id>();

    accIdAndOppMap = new Map<Id,Opportunity>(); 

    for(Lead newLeadRec : trigger.new){

        String locationInfo = '';
        newLeadRec.Location__c = NULL;
        newLeadOwnerIdSet.add(newLeadRec.OwnerId);

        if(newLeadRec.Phone != NULL ){

          phoneIdSet.add(newLeadRec.Phone);

           newLeadRec.Phone_external__c = newLeadRec.Phone;
        }

        if(newLeadRec.Email != NULL && newLeadRec.Email != ''){
            emailIdSet.add(newLeadRec.Email.toLowerCase());
        }

        if(newLeadRec.firstName != NULL && newLeadRec.firstName != '') {
            locationInfo += newLeadRec.firstName;
            firstNameSet.add(newLeadRec.firstName.toLowerCase());
        }

        if(newLeadRec.LastName != NULL && newLeadRec.LastName != ''){
            locationInfo += newLeadRec.LastName;
            lastNameSet.add(newLeadRec.LastName.toLowerCase());
        }

        if(newLeadRec.City!= NULL && newLeadRec.City != ''){
            locationInfo += newLeadRec.City;
            citySet.add(newLeadRec.City.toLowerCase());
        }
        if(newLeadRec.State!= NULL && newLeadRec.State != ''){
            locationInfo += newLeadRec.State;
            stateSet.add(newLeadRec.state.toLowerCase());
        }

       if(locationInfo != NULL && locationInfo != ''){
           newLeadRec.Location__c = locationInfo;
           locationSet.add(locationInfo.toLowerCase());

       }
    }

if(phoneIdSet.size() > 0 || emailIdSet.size() > 0 || locationSet.size() > 0 ||  firstNameSet.size() > 0  || lastNameSet.size() > 0 || 
                        citySet.size() > 0 || stateSet.size() > 0 ){

     for(Lead ExistingLeadRec : [SELECT Id,FirstName,LastName,Name,Email,Phone,City,State,Description,Location__c,
                                        RecordTypeId,Status,OwnerId,CreatedDate,Owner.isActive
                                     FROM LEAD WHERE DuplicatesLead__c = null
                                     AND (Phone_external__c IN :phoneIdSet OR
                                       Email IN :EmailIdSet OR
                                       Location__c IN :locationSet)
                                   ORDER BY CreatedDate ASC
                                   ]){


            if(ExistingLeadRec.Phone != NULL && ExistingLeadRec.Phone != '' ){


                if(!PhoneAndExistingLeadMap.containsKey(pho)){
                    PhoneAndExistingLeadMap.put(pho,ExistingLeadRec);
                }                   
            }

            if(ExistingLeadRec.Email != NULL && ExistingLeadRec.Email != '' && 
                   !EmailAndExistingLeadMap.containsKey(ExistingLeadRec.Email.toLowerCase()) ){
               EmailAndExistingLeadMap.put(ExistingLeadRec.Email.toLowerCase(),ExistingLeadRec);    
            }


            if(ExistingLeadRec.Location__c != NULL && ExistingLeadRec.Location__c != '' ){
                String MapKey = ExistingLeadRec.Location__c.toLowerCase();
                if(MapKey != NULL && MapKey != '' && !nameLocationAndExistingLeadMap.containsKey(MapKey) ){
                    nameLocationAndExistingLeadMap.put(MapKey,ExistingLeadRec);       
                } 

            }
        }

    for(Lead newLeadRec : trigger.new){

          if(newLeadRec.Phone != NULL && newLeadRec.Phone != ''){


               if(PhoneAndExistingLeadMap.containsKey(pho) && PhoneAndExistingLeadMap.get(pho) != NULL ) {

                    newLeadRec.DuplicatesLead__c = PhoneAndExistingLeadMap.get(pho).Id;
                  }

             }

            if(newleadRec.Email != NULL && newLeadRec.Email != ''  && EmailAndExistingLeadMap.containsKey(newLeadRec.Email.toLowerCase()) && EmailAndExistingLeadMap.get(newLeadRec.Email.toLowerCase()) != NULL ){

                   newLeadRec.DuplicatesLead__c = EmailAndExistingLeadMap.get(newLeadRec.Email.toLowerCase()).Id;

              }

           if(newLeadRec.Location__c != NULL && newLeadRec.Location__c != '' && nameLocationAndExistingLeadMap.containsKey(MapKey.toLowerCase())&&
                               nameLocationAndExistingLeadMap.get(MapKey.toLowerCase()) != NULL) {

                  newLeadRec.DuplicatesLead__c = nameLocationAndExistingLeadMap.get(MapKey.toLowerCase()).Id;

               }
      }
  }

}
2
  • n.b. when I first read this, I thought you were trying to run Lead Assignment rules on duplicate leads; perhaps a better trigger name would be LeadDuplicateAssociation or LeadDuplicateChaining?
    – cropredy
    Commented Mar 21, 2014 at 18:31
  • @crop1645 Thanks I have updated my trigger name
    – sfdc
    Commented Mar 21, 2014 at 18:44

1 Answer 1

3

There are 2 options:

  1. besides doing a query for existing Leads, also compare all the Leads in the trigger with each other.
  2. move your logic to the afterTrigger. All Leads will be included in the query results then (also the ones in the trigger). Don't forget to filter out the comparison of the current Lead with itself when looping through them for matching :-) Then do a specific Update on the new Leads that require the DuplicatesLead__c to be filled.

I usually prefer option 2, since it feels a bit safer having everything in 1 set of query results. Drawback is the extra Update statement at the end, but I accept that over having to incorporate the extra logic of option 1.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .