05.11.2014

Salesforce How to Dedup Your Leads



salesforce-cloud

Couple of weeks ago we were looking for a solution how to de-duplicate leads against contact email addresses. The trigger needed to be bulkified and we wanted to have error shown in standard error message place, when saving lead. We also wanted to give user a link to click to check on the existing contact. Together with our customer we come to an agreement that only almost unique attribute of a lead is an email address. We were thinking about using first names and last names together with company name as well but the complexity grow too much.

Note: you can very easily add check for lead email addresses if you would like to.

Note II: the source code can be found here.

The trigger we have created will stop update on leads that have same email address as any contact in your SF instance. Unique leads will be updated.

trigger LeadDedup on Lead (before insert, before update) {

    //this trigger will stop mass update of leads, that have duplicated email address with contact
    String sfdcURL = URL.getSalesforceBaseUrl().toExternalForm();
    List<String> emailList = new List<String>();
    Map<String, Contact> duplMap = new Map<String, Contact>();
    String name;
    Contact tempCont;

We are using before insert, before update trigger, to prevent saving of a lead that is violating our rules.

The variable sfdcURL is used to get the actual SF instance URL to be used in link to existing contact.

List emailList is storing all emails of the leads that are being updated or inserted and are within specified rules. You cannot de-duplicate converted leads, as there will always be a contact with the email address same as a lead has.

//adding all leads from trigger that have email and are not converted to the list
for (Lead lead : Trigger.new) {
    if (lead.Email != null && lead.IsConverted == FALSE) {
        emailList.add(lead.Email);
    }
}

Within Map duplMap we store all contacts that have same emails as any lead that is being updated or inserted. As we are using map, we need to make sure not to insert any duplicated email here. So in case that there are multiple contacts with same email we will be working only with one.

//itterate over contacts email addresses and adding any matches to the map,
//exluding already added emails
for (Contact cont : [SELECT Id, FirstName, LastName, Email FROM Contact WHERE Email IN :emailList]) {
    if (!duplMap.containsKey(cont.Email)) {
        duplMap.put(cont.Email, cont);
    }
}

And now we just iterate over the trigger.new and in case there is duplicity we will show an error message.

//now we compare each lead with the map of matching contacts.
//if there is a match the user will get the error message with
//the link to matching contact
for (Lead lead : Trigger.new) {
    tempCont = duplMap.get(lead.Email);
    if(tempCont == null)
        continue;

    if (tempCont.firstName == null) {
        name = tempCont.LastName;
    } else {
        name = tempCont.FirstName + ' ' + tempCont.LastName;
    }
    lead.addError('<br/>Duplicate contact found!<br/><br/>Please check on contact ' +
        '<a href="' + sfdcURL + '/' + tempCont.Id + '" target="_blank">'+ name +'</a>', false);
    }
}