Salesforce How to Dedup Your Leads


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.

  1. trigger LeadDedup on Lead (before insert, before update) {
  2. //this trigger will stop mass update of leads, that have duplicated email address with contact
  3. String sfdcURL = URL.getSalesforceBaseUrl().toExternalForm();
  4. List<String> emailList = new List<String>();
  5. Map<String, Contact> duplMap = new Map<String, Contact>();
  6. String name;
  7. 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.

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

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.

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

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

  1. //now we compare each lead with the map of matching contacts.
  2. //if there is a match the user will get the error message with
  3. //the link to matching contact
  4. for (Lead lead : {
  5. tempCont = duplMap.get(lead.Email);
  6. if(tempCont == null)
  7. continue;
  8. if (tempCont.firstName == null) {
  9. name = tempCont.LastName;
  10. } else {
  11. name = tempCont.FirstName + ‘ ‘ + tempCont.LastName;
  12. }
  13. lead.addError(‘<br/>Duplicate contact found!<br/><br/>Please check on contact ‘ +
  14. ‘<a href=”‘ + sfdcURL + ‘/’ + tempCont.Id + ‘” target=”_blank”>’+ name +'</a>’, false);
  15. }
  16. }

Increase your competitive advantage.

We will direct you in the right direction!

Contact us