Advertisement
aenila

JobAssessment - Duplicate Check

Nov 20th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class checkDupes {
  2.  
  3.     public static void findDupes(){
  4.         //create map to store lead info by email
  5.         Map<String, Lead> leadMap = new Map<String, Lead>();
  6.         //iterate through leads to populate map
  7.         for(Lead ld : (List<Lead>)trigger.new){
  8.             if(ld.email != null)
  9.             leadMap.put(ld.email, ld);
  10.         }
  11.        
  12.         //loop through all contacts where a matching email was found and
  13.         //tie it to a particular lead and then error that lead out
  14.         //skip process if leadMap is empty
  15.         if(!leadMap.isEmpty()){
  16.             for(Contact con: [SELECT id, email, firstName, lastName FROM Contact WHERE email IN :leadMap.keyset()]){
  17.                 if(leadMap.containsKey(con.email))
  18.                     leadMap.get(con.email).addError('A contact already exists with this email: '+con.firstName+' '+con.lastName+
  19.                                                 '<br/> <a href="https://kiterealty--casestudy.cs26.my.salesforce.com/'+
  20.                                                 con.id+'">Click here to view</a>', false);
  21.             }
  22.         }
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement