Create a map for duplicate contacts by phone or email id
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
Map<String,List<Contact>> phoneMap = new Map<String, List<Contact>>(); List<Contact> scope = [SELECT Id,Phone,Email,CreatedDate FROM Contact WHERE (Phone!='' OR Email!='') order by createddate desc]; for(Contact ct:scope) { String phone = String.valueOf(ct.Phone); String email = String.valueOf(ct.Email); if(String.isNotBlank(String.valueOf(ct.Phone))) { phone = phone.replace('-', '').replace('(', '').replace(')', '').replace(' ', '').replace(',', ''); if (phoneMap.containsKey(phone)) { List<contact> c = phoneMap.get(phone); c.add(ct); } else { phoneMap.put(phone, new List<Contact>{ ct }); } } else{ if (emailMap.containsKey(email)) { List<contact> c = emailMap.get(email); c.add(ct); } else { emailMap.put(email, new List<Contact>{ ct }); } } } for(String key:phoneMap.keySet()) { List<Contact> ct = phoneMap.get(key); if(ct.size()>1) { System.debug('phoneMap: ' + key + ': ' + ct.size() + ': ' + phoneMap.get(key)); } } for(String key:emailMap.keySet()) { List<Contact> ct = emailMap.get(key); if(ct.size()>1) { System.debug('emailMap: ' + key + ': ' + ct.size() + ': ' + emailMap.get(key)); } } |