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
Allen2Allen2 

Stop creation of duplicate contact on lead conversion

I've written the below piece of code and called in after update context on lead but doesn't work. Can anyone help me to correct the code.
public class LeadHandler {
    public static void dedupeContactOnLeadConversion(List<Lead> newList,map<id, 
    Lead> oldLeadMap){
        List<String> email = new List<String>();
        List<String> firstName = new List<String>();
        List<String> lastName = new List<String>();
        
        for(Lead objLead: newList){
            if(!String.ISBLANK(objLead.Email) && objLead.IsConverted == false){
                email.add(objLead.Email);
            }
            if(!String.ISBLANK(objLead.FirstName) && objLead.IsConverted == false){
                firstName.add(objLead.FirstName);                
            }
            if(!String.ISBLANK(objLead.LastName) && objLead.IsConverted == false){
                lastName.add(objLead.LastName);   
            }
        }
        
        List<Contact> objContact = [Select Id, Email, FirstName, LastName from Contact where Email in: email OR FirstName in: firstName OR LastName in: lastName];
        
        for(Contact newContact: objContact){
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setContactId(newContact.Id);
            Database.LeadConvertResult listLeadConvertResult = Database.convertLead(lc, false);
        }
    }
}

I only want to write a trigger which will stop the creation of duplicate contact only when you're converting the lead.
ANUTEJANUTEJ (Salesforce Developers) 
Hi there,

Have you tried checking if it is working, you could test it in an anonymous window and see if it works?

Do let me know if it works and this is just a thought but as you are trying to stop the duplicate lead creation  wouldn't it be better to do it on before insert or before update?

I hope this helps.

Regards,
Anutej
ANUTEJANUTEJ (Salesforce Developers) 
Aditionally, I would also suggest you to please have a look at the below OOB functionality documentation I found:

>>https://help.salesforce.com/articleView?id=duplicate_rules_standard_lead_rule.htm&type=5

>> https://help.salesforce.com/articleView?id=duplicate_prevention.htm&type=5

I hope this helps.
Allen2Allen2
I'm aware of duplicate rule functionality but duplicate rule never work for lead conversion. So, I went for trigger. You can explore a bit more.
ANUTEJANUTEJ (Salesforce Developers) 
I tried checking this and I found that there is something called "Require Validation for Converted Leads" can you try checking the below article regarding the same: https://help.salesforce.com/articleView?id=000333493&type=1&mode=1 Do let me know if it works.
Allen2Allen2
Yep that is the configuration under Lead Settings and which is already enabled in my org.
ANUTEJANUTEJ (Salesforce Developers) 
Oh, and is it working? and apart from that I also found a trigger implementation similar to your use case that could help, 

Link: https://salesforce.stackexchange.com/questions/14604/trigger-to-check-for-duplicate-contacts-on-lead-conversion
 
trigger BlockDupeLeadConvert on Lead (after update) {
    class UniqueKey {
        UniqueKey(string Firstname, String lastName, String email) {
            this.firstname = firstname;
            this.lastname = lastname;
            this.email = email;
        }
        string FirstName, LastName, Email;
        integer hashCode() {
            return toString().hashCode();
        }
        boolean equals(Object o) {
            return toString() == ((UniqueKey)o).toString();
        }
        public override string toString() {
            return String.format(
                'FirstName = \'\'{0}\'\' AND LastName = \'\'{1}\'\' AND Email = \'\'{2}\'\'',
                new String[] {
                    FirstName==null?'':String.escapeSingleQuotes(FirstName.tolowercase()),
                    LastName==null?'':String.escapeSingleQuotes(LastName.tolowercase()),
                    Email==null?'':String.escapeSingleQuotes(Email.tolowercase())
                }
            );
        }
    }

    Map<UniqueKey, Lead> leads = new Map<UniqueKey, Lead>();
    for(Lead record:Trigger.new) {
        if(record.isconverted) {
            UniqueKey key = new UniqueKey(record.firstname, record.lastname, record.email);
            if(leads.containskey(key)) {
                record.addError('Duplicate lead conversion.');
            } else {
                leads.put(key, record);
            }
        }
    }
    String[] keys = new String[0];
    for(UniqueKey key:leads.keyset()) {
        keys.add(key.tostring());
    }
    String query = string.format(
        'SELECT Id,FirstName,LastName,Email FROM Contact WHERE {0}',
        new String[] {
            String.join(keys, ' OR ')
        }
    );
    if(!leads.isempty()) {
        for(Contact record:Database.query(query)) {
            UniqueKey key = new UniqueKey(record.firstname, record.lastname, record.email);
            if(leads.containskey(key)) {
                leads.get(key).addError('Duplicate lead conversion.');
            }
        }
    }
}

Above is the code for quick reference. Also, a note was mentioned below the implementation do check it according to your use case.