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
MRDJMRDJ 

Trigger to avoid duplicate records

Hello,

I have followed the below code not to allow duplicates. But I don't want to display the error message. If there is duplicate record is created or existing record updated then shouldn't throw error simply skip that record. I mean do not insert the record in the object. 

Can someone help me
 
trigger contactDuplicatePreventer on Contact(before insert, before update) {

    Map<String, Contact> contactMap = new Map<String, Contact>();
    for (Contact Contact : System.Trigger.new) {
		
        // Make sure we don't treat an email address that  
        // isn't changing during an update as a duplicate.  
    
        if ((Contact.Email != null) &&
                (System.Trigger.isInsert ||
                (Contact.Email != 
                    System.Trigger.oldMap.get(Contact.Id).Email))) {
		
            // Make sure another new Contact isn't also a duplicate  
    
            if (contactMap.containsKey(Contact.Email)) {
                Contact.Email.addError('Another new Contact has the '
                                    + 'same email address.');
            } else {
                contactMap.put(Contact.Email, Contact);
            }
       }
    }
	
    // Using a single database query, find all the Contacts in  
    
    // the database that have the same email address as any  
    
    // of the Contacts being inserted or updated.  
    
    for (Contact contact : [SELECT Email FROM Contact
                      WHERE Email IN :contactMap.KeySet()]) {
        Contact newContact = contactMap.get(Contact.Email);
        newContact.Email.addError('A Contact with this email '
                               + 'address already exists.');
    }
}

 
Marcilio SouzaMarcilio Souza
Hello,

For you remove the erro message comment theses lines:
Contact.Email.addError('Another new Contact has the '  + 'same email address.');
newContact.Email.addError('A Contact with this email '  + 'address already exists.');

The trigger is check if there are Contacts with the same e-mail address.

Best regrets,
Marcilio
 
MRDJMRDJ
Hi Amit,
Thanks for your reply.
In your code, in line 28 still the error message is displayed.
Amit Chaudhary 8Amit Chaudhary 8
Please try below code
trigger contactDuplicatePreventer on Contact(before insert, before update) 
{
	Set<String> setEmailID = new set<String>();
	Set<Id> setContID = new set<ID>();
    for (Contact Contact : System.Trigger.new) 
	{
        if ((Contact.Email != null) &&  (System.Trigger.isInsert ||  (Contact.Email != System.Trigger.oldMap.get(Contact.Id).Email))) 
		{
			setEmailID.add(Contact.Email);
			setContID.add(Contact.id);
		}
    }

	List<Contact> lstCOntact = [select id ,email from contact where email in :setEmailID and id not in :setContID ];
    Map<String, Contact> contactMap = new Map<String, Contact>();

	for(Contact cont : lstCOntact)
	{
		contactMap.put(cont.email, cont);
	}	

    for (Contact Contact : System.Trigger.new) 
	{
        if ((Contact.Email != null) &&  (System.Trigger.isInsert ||  (Contact.Email != System.Trigger.oldMap.get(Contact.Id).Email))) 
		{
			if(contactMap.containsKey(Contact.Email))
			{
				Contact.Email.addError('A Contact with this email address already exists.');
			}
		}	
	}	
}

 
MRDJMRDJ
Hi Amit,

I have updated the code based on your inputs. I could still see the error message.
What I am looking is if user enter new contact with same email, then the new contact record shouldn't throw error message. Without throwing error message that record should be cleaned.
 
Amit Chaudhary 8Amit Chaudhary 8
you canot skip the DML(insert/update) without throwing any error .