Thursday, 25 June 2015

Prevent Duplicate Records in Salesforce by Comparing more than one fields


Quick Summary on Logic:

      The below trigger written in Lead object using equals and hashCode Method. This methods are used on uniqueness.  Each hashCode value is unique. 

For example: test@mail.com and Test@mail.com will provide different hashCode values.

Object: Lead
Logic: If Lead record contains same Email and Company Name, then the below trigger will prevent duplication.


Apex Class

  1. public class DeDupeWrapper
  2. {
  3.     public String email;
  4.     public String company;
  5.    
  6.     public DeDupeWrapper(String email, String company)
  7.     {
  8.         this.email = email != null ? email.toLowerCase() : email;
  9.         this.company = company != null ? company.toLowerCase() : company;
  10.     }
  11.    
  12.     public Boolean equals(Object obj)
  13.     {
  14.         if (obj instanceof DeDupeWrapper)
  15.         {
  16.             DeDupeWrapper p = (DeDupeWrapper)obj;
  17.             return ((email == p.email) && (company == p.company));
  18.         }
  19.         return false;
  20.     }
  21.    
  22.     public Integer hashCode()
  23.     {
  24.         return (email.hashCode() * company.hashCode());
  25.     }
  26. }


Apex Trigger

  1. trigger DeDuplicateLead on Lead(before insert, before update)
  2. {
  3.     Map<DeDupeWrapper, Lead> leadMap = new Map<DeDupeWrapper, Lead>();
  4.     Map<String, String> duplicateMap = new Map<String, String>();
  5.    
  6.     for (Lead currLead: Trigger.new)
  7.     {
  8.       if((Trigger.isInsert && (currLead.Email != null && currLead.company != null))  ||
  9.            (Trigger.isUpdate && (currLead.Email != Trigger.oldMap.get(currLead.Id).Email ||
  10.                                  currLead.Company != Trigger.oldMap.get(currLead.Id).Company)))
  11.         {
  12.            
  13.             DeDupeWrapper key = new DeDupeWrapper(currLead.Email, currLead.Company);
  14.            
  15.             if (leadMap.containsKey(key))
  16.             {
  17.                 currLead.Email.addError('DUPLICATE CAUGHT...!');
  18.             }
  19.             else
  20.             {
  21.                 leadMap.put(key, currLead);
  22.             }
  23.         }
  24.     }
  25.    
  26.     for(DeDupeWrapper currWrap : leadMap.keySet())
  27.     {
  28.         duplicateMap.put(currWrap.Email, currWrap.company);
  29.     }
  30.    
  31.     for (Lead lead : [SELECT Email,Company FROM Lead WHERE Email in: duplicateMap.keySet() AND Company in: duplicateMap.values()])
  32.     {
  33.         DeDupeWrapper key = new DeDupeWrapper(lead.Email, lead.Company);
  34.         Lead newLead = leadMap.get(key);
  35.         if(newLead != null)
  36.         {
  37.             newLead.addError('DUPLICATE RECORD ALREADY FOUND');
  38.         }
  39.     }
  40. }

No comments:

Post a Comment

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...