function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
chandu kumarchandu kumar 

hi,i would like to creat a new trigger on contact object that do not allow the duplicate mobile numbers

Best Answer chosen by chandu kumar
Deepak GulianDeepak Gulian
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.');
    }
}

 

All Answers

Deepak GulianDeepak Gulian
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.');
    }
}

 
This was selected as the best answer
chandu kumarchandu kumar
Hello deepak,
i tried your trigger code ,but it allowing save records with duplicate mobile numbers
Deepak GulianDeepak Gulian
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!
 
chandu kumarchandu kumar
yes yes......i got it.thank you