[SalesForce] Lead Trigger to prevent duplicates

I'm trying to create some code to prevent the creation of duplicate leads when there is already an existing contact in the database with the same email address. Not only this, but, if a matching contact is found, I want to update that contact instead of inserting the lead. I can successfully add an error to prevent the lead from being created, but I can't seem to update the contact.

Reading this: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_bulk_exceptions.htm

It seems that Salesforce rolls back all DML operations when there is an error on the record. I thought if I tried the update contact DML before adding the error to the lead record that this might solve the problem, but I have been unsuccessful. Any ideas on how to accomplish this?

Trigger Handler Class below:

public class LeadTriggerHandler {

    /********************************************************
     * Before Insert
     * *****************************************************/

    public static void handleBeforeInsert(List<Lead> newLeads) {
        //Do some before insert stuff
        checkForDupes(newLeads);

    }//End of method

    /********************************************************
     * 
     * Private Methods
     * *****************************************************/

    private static void checkForDupes(List<Lead> newLeads){
        Map<String,Lead> leadMap = new Map<String,Lead>();
        for(Lead l : newLeads){
            if(l.Email != null && !leadMap.containsKey(l.Email)){
            leadMap.put(l.Email,l);
            }//end of if
            else if(l.Email == null){
                //do nothing
            }//end of else if
            else{
                l.Email.addError('Another lead in this batch has the same Email Address');
            }//end of else
        }//end of for loop
        if(leadMap.size()>0){
            List<Contact> queriedContacts = new List<Contact>([Select Id, Email from Contact Where Email in :leadMap.keySet()]);
            if(queriedContacts.size()>0){
                List<Contact> contactsToUpdate = new List<Contact>();
                Map<String,Contact> matchingContacts = new Map<String,Contact>();
                for(Contact con : queriedContacts){
                    matchingContacts.put(con.Email,con);
                }//end of for loop
                for(Lead l2 : newLeads){
                    //do stuff
                    if(matchingContacts.containsKey(l2.Email)){
                        //handle dupes
                                contactsToUpdate.add(new Contact(
                                Id = matchingContacts.get(l2.Email).Id,
                                Do_Not_Sync_w_Eloqua__c = false
                            ));
                    }//end of containsKey if
                }//end of for loop
                Database.update(contactsToUpdate, false);
                for(Lead l3 : newLeads){
                    if(matchingContacts.containsKey(l3.Email)){
                        l3.Email.addError('A contact with this email already exists in the system with ID of '+matchingContacts.get(l3.Email).Id);
                    }
                }//end of l3 for loop
            }//end of queriedContacts.size()>0 if
        }//end of leadMap.size()>0 if
    }//end of method
}

Best Answer

Why dont you give a shot at Data.com Duplicate Management, which was recently introduced by Salesforce. You can set your criteria and salesforce would identify duplicates for you.

I think its an apt candidate for such issues. Can save you a lot of time and effort. Data.com Duplicate management

Secondly can you confirm whether the actual DML operation which invoked the trigger is making use of Database.upsert or something like that. If not try that, should work in that case.

Related Topic