trigger contactDuplicatePreventer on Contact (before insert, before update) {
Map<String, Contact>conMap = new Map<String, Contact>();
for (Contact con : System.Trigger.new) {
if ((con.Phone != null) &&
(System.Trigger.isInsert ||
(con.Phone !=
System.Trigger.oldMap.get(con.Id).Phone))) {
// Make sure another new contact isn't also a duplicate
if (conMap.containsKey(con.Phone)) {
con.Phone.addError('Another new Contact has the '
+ 'same phone number.');
} else {
conMap.put(con.Phone, con);
}
}
}
for (Contact con : [SELECT Phone FROM Contact
WHERE Phone IN :conMap.KeySet()]) {
Contact newCon = conMap.get(con.Phone);
newCon.Phone.addError('A Contact with this phone'
+ 'number already exists.');
}
}
trigger contactDuplicatePreventer on Contact (before insert, before update) {
Map<String, Contact>conMap = new Map<String, Contact>();
for (Contact con : System.Trigger.new) {
if ((con.Phone != null) &&
(System.Trigger.isInsert ||
(con.Phone !=
System.Trigger.oldMap.get(con.Id).Phone))) {
// Make sure another new contact isn't also a duplicate
if (conMap.containsKey(con.Phone)) {
con.Phone.addError('Another new Contact has the '
+ 'same phone number.');
} else {
conMap.put(con.Phone, con);
}
}
}
for (Contact con : [SELECT Phone FROM Contact
WHERE Phone IN :conMap.KeySet()]) {
Contact newCon = conMap.get(con.Phone);
newCon.Phone.addError('A Contact with this phone'
+ 'number already exists.');
}
}
trigger contactDuplicatePreventer on Contact (before insert, before update) {
Map<String, Contact>conMap = new Map<String, Contact>();
for (Contact con : System.Trigger.new) {
if ((con.MobilePhone != null) &&
(System.Trigger.isInsert ||
(con.MobilePhone !=
System.Trigger.oldMap.get(con.Id).MobilePhone))) {
// Make sure another new contact isn't also a duplicate
if (conMap.containsKey(con.MobilePhone)) {
con.MobilePhone.addError('Another new Contact has the '
+ 'same phone number.');
} else {
conMap.put(con.MobilePhone, con);
}
}
}
for (Contact con : [SELECT MobilePhone FROM Contact
WHERE MobilePhone IN :conMap.KeySet()]) {
Contact newCon = conMap.get(con.MobilePhone);
newCon.MobilePhone.addError('A Contact with this phone'
+ 'number already exists.');
}
}
This one is for Mobile Field, earlier trigger was working for Phone field!
All Answers
i tried your trigger code ,but it allowing save records with duplicate mobile numbers