Thursday 6 November 2014

Salesforce Lead Duplication

  1. trigger leadDuplicatePreventer on Lead
  2.                                (before insert, before update) {
  3.  
  4.     Map<String, Lead> leadMap = new Map<String, Lead>();
  5.     for (Lead lead : System.Trigger.new) {
  6.                
  7.         // Make sure we don't treat an email address that  
  8.    
  9.         // isn't changing during an update as a duplicate.  
  10.    
  11.         if ((lead.Email != null) &&
  12.                 (System.Trigger.isInsert ||
  13.                 (lead.Email !=
  14.                     System.Trigger.oldMap.get(lead.Id).Email))) {
  15.                
  16.             // Make sure another new lead isn't also a duplicate  
  17.    
  18.             if (leadMap.containsKey(lead.Email)) {
  19.                 lead.Email.addError('Another new lead has the '
  20.                                     + 'same email address.');
  21.             } else {
  22.                 leadMap.put(lead.Email, lead);
  23.             }
  24.        }
  25.     }

No comments:

Post a Comment