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
Vandana RattanVandana Rattan 

Duplicate Contacts on Lead Convert

I am trying to get a list of duplicate contacts on Lead Conversion. This is the trigger I have written so far.
trigger LeadConvert on Lead (after update) {
 
    if(trigger.new.size() > 1) 
            return;
     
    if (trigger.old[0].isConverted == false && trigger.new[0].isConverted == true) {
        
        //Block for deduping Contacts
        if (Trigger.new[0].ConvertedContactId != null) {
            System.debug('Converted ontact Id >>> ' + Trigger.new[0].ConvertedContactId);
            System.debug('trigger.old[0]>>>' +trigger.old[0]);
            System.debug('trigger.old[0]>>>' +trigger.new[0]);
            List<Contact> lstDupContact = [SELECT id, firstName, lastName, email from Contact where (firstName =: Trigger.new[0].FirstName OR
                                          lastName =: Trigger.new[0].LastName) AND email =: Trigger.new[0].email];
            
            
            
            for(Contact c: lstDupContact){
            
            
            System.debug('lstDupContact >>>>' + lstDupContact);
            Lead oldRec = Trigger.old[0];
                oldRec.Name.addError('Duplicate Contacts Found!!');
                //oldRec.addError('Duplicate Contacts Found!!');
            }
            
        }
       
        
    }
}
The lstDupContact now contains all duplicate contacts. How can I display this list to users? I tried displaying error message using oldRec.Name.addError('Duplicate Contacts Found!!'); but was unable to do so. 

Basically I want my duplicate contacts list to be displayed to users. Kindly suggest as it is urgent.

Thanks
bob_buzzardbob_buzzard
As you are in a trigger you don't really have a way to pass information back to the user, outside of the addError function.  If you can change your trigger to a before update, you'll be able to use record.Name.addError.  If you can't do that, your only option is to throw an exception I'd say.

To display the list to the user you'd need to move this logic into a Visualforce controller and override the lead conversion, which is probably more trouble than its worth for something like this.
Vandana RattanVandana Rattan
Thanks for your Reply Bob. I have modified my trigger a bit. It looks like:-

trigger LeadConvertTrigger on Lead ( before update) {
    Boolean dupFound = false;
    List<String> lstDupNames = new List<String>();
    
    if(trigger.new.size() > 1) 
            return;
    
    if ( trigger.new[0].isConverted == true) {
        System.debug('Inside If>>>');
        //Block for deduping Contacts
        if (Trigger.new[0].ConvertedContactId != null) {
         List<Contact> lstDupContact = [SELECT id, Name, EA_Member_No__c from Contact where (firstName =: Trigger.new[0].FirstName OR
                                          lastName =: Trigger.new[0].LastName) AND email =: Trigger.new[0].email AND id !=: Trigger.new[0].ConvertedContactId];
            
            //lstDupContact.remove(lstDupContact.size()-1);
            if(lstDupContact.size() > 0){
                dupFound = true;
             
            for(Contact c: lstDupContact){
                  
              lstDupNames.add(c.Name);
               Trigger.new[0].addError('Duplicate Contacts Found with Names: ' + lstDupNames + 'Try changing the First Name OR Last Name AND Email ID');
                //throw new Exception('Duplicate Contacts Found with Name: ' + c.Name);
                //for(LeadCon)
                
       
            }
            }
         
            
        }
}
}
However, the error is always shown below the ConvertedStatus field as shown in the scheenshot attached. How can I make the error appear on top of the page instead?
Error Screenshot

Thanks In Advance.
Vandana RattanVandana Rattan
Also Bob could you give some suggestions on how can I go about bulkifying my trigger.
bob_buzzardbob_buzzard
I wouldn't expect the error to be displayed against a particular field unless you have added the error to the field rather than the record as whole.
Vandana RattanVandana Rattan
Thanks Bob. As Trigger resuls were not that user friendly, I had to go for the longer solution of creating Custom Lead Convert Page and Controller. I am facing an issue in it. I have posted my query on a separate thread on the below link:-

https://developer.salesforce.com/forums/ForumsMain?id=906F000000098FfIAI#!/feedtype=SINGLE_QUESTION_DETAIL&dc=Visualforce_Development&criteria=OPENQUESTIONS&id=906F0000000AiuWIAS