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
shobana shobana 1shobana shobana 1 

Trigger to throw an error message for duplicate email id

In Lead and contact   object i have a trigger which  check if there is any duplicate email Id. If it has duplicate it throw an error "Provided email id already exist for another record". This scenario is working fine.

But in the lead conversion if i try to merge twith exiting account and contact  record   its throwing an error like "Error: Validation error on Contact: Error:Provided email id already exist for another record"

 
Ajay mishraAjay mishra
Hi Shobana,

Can you share your code, so can check and suggest.
bob_buzzardbob_buzzard
So is the issue that the error message is different? I think you'll have to live with this. The underlying error is the same, but as the lead conversion is creating an account, contact and potentially an opportunity, it has to give more context about where the error originated.
shobana shobana 1shobana shobana 1
Hi 
Thank you for your reply. My code is
trigger ContactDuplicateEmailPreventor on Contact(before insert, before update) 
{
     Set<Id> oppIds = new Set<Id>();
    Map<String, Contact> mapEmailToContact = new Map<String, Contact>();
for(Contact objContact : Trigger.New) 
    {   
         If(Trigger.IsUpdate)
          oppIds.add(objContact.Id);
        if((String.isNotBlank(objContact.Email) && String.isNotBlank(objContact.Alternate_Email1__c) && objContact.Email == objContact.Alternate_Email1__c)
            || 
            (String.isNotBlank(objContact.Alternate_Email1__c) && String.isNotBlank(objContact.Alternate_Email_2__c) && objContact.Alternate_Email1__c == objContact.Alternate_Email_2__c)
            ||
            (String.isNotBlank(objContact.Email) && String.isNotBlank(objContact.Alternate_Email_2__c) && objContact.Email == objContact.Alternate_Email_2__c)
            )
        {    
            objContact.addError('Error: Entered emails cant be same');
        }
        
        if(String.isNotBlank(objContact.Email))
        {
            if(!mapEmailToContact.containsKey(objContact.Email))
                mapEmailToContact.put(objContact.Email, objContact);
        }
        
        if(String.isNotBlank(objContact.Alternate_Email1__c))
        {
            if(!mapEmailToContact.containsKey(objContact.Alternate_Email1__c))
                mapEmailToContact.put(objContact.Alternate_Email1__c, objContact);
        }
        
        if(String.isNotBlank(objContact.Alternate_Email_2__c))
        {
            if(!mapEmailToContact.containsKey(objContact.Alternate_Email_2__c))
                mapEmailToContact.put(objContact.Alternate_Email_2__c, objContact);
        }
        
    }
    
    for(Contact objExistingContact : [SELECT Email, Alternate_Email1__c,Alternate_Email_2__c
                                FROM contact
                                WHERE (Email IN : mapEmailToContact.keySet() 
                                    OR Alternate_Email1__c IN : mapEmailToContact.keySet()
                                    OR Alternate_Email_2__c IN : mapEmailToContact.keySet())  AND Id NOT IN:oppIds
                                ])
    {
        if(mapEmailToContact.containsKey(objExistingContact.Email))
        {
            mapEmailToContact.get(objExistingContact.Email).addError('Error:Provided email id already exist for another record');
        }
        
        if(mapEmailToContact.containsKey(objExistingContact.Alternate_Email1__c))
        {
            mapEmailToContact.get(objExistingContact.Alternate_Email1__c).addError('Error:Provided email id already exist for another record');
        }
        
        if(mapEmailToContact.containsKey(objExistingContact.Alternate_Email_2__c))
        {
            mapEmailToContact.get(objExistingContact.Alternate_Email_2__c).addError('Error:Provided email id already exist for another record');
            system.debug('shobana' + mapEmailToContact);
        }
    }

}

 
shobana shobana 1shobana shobana 1
Hi Bob
 I think error is because of the trigger  in  contact .
 
Ajay mishraAjay mishra
Hi Shobana,

Trigger is working correctly as conditions passed in it. Deactivate trigger and try merging it will work. 

As your trigger is working on before Update it will find the Duplicate Emails Id according to your trigger.
shobana shobana 1shobana shobana 1
Hi Ajay

Thank you for your reply. If i deactivate the trigger its working. So what would be the solution to rectify the isssue.
Ajay mishraAjay mishra
Hi Shobana,

Use masterRecordId =: null in the Query. Which will return Non Merging Records.