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
Charni Wiggins 9Charni Wiggins 9 

Convert lead trigger if Contact already exists

Hi,

We have many web-to-lead forms and many repeat customers, it would be great to have a trigger which checks if the lead already exists as a contact, and if so convert the lead and update contact if needed. 
Apologies for the bad code, I am an admin so struggling to get this right. 

Here is my attempt so far.. 

trigger ConvertLead on Lead (before insert) {

    // every new lead - check if exists as contact via email
    List<String> leadEmails = new List<String>();

    for(Lead lead:Trigger.new){
        leadEmails.add(lead.Email);
        
    }

    // Search existing contacts with matching email
    List<Contact> contacts = [
        SELECT 
            Id, Email 
        FROM 
            Contact
        WHERE 
            Email IN :leadEmails
    ];

    // set of con emails if exists - create map instead? con id to lead id 
    Set<Id> contactIds = new Set<Id>();
    for(Contact contact:contacts){
        contactIds.add(contact.Id);
    }

    Map<Id, Id> conIds = new Map<Id, Id>();
    
    
    for(Lead lead:Trigger.new){
        // if cons exists - map con id to lead converted con id? - setcontactId
        //                       - set lead to converted - bypass GDPR rules
        if(contactIds.contains(lead.Email)){            
			//LeadConvert.setContactId(contactIds);
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(lead.Id);
            lc.setContactId(conIds.get(contactIds).Id);
        }
    }
    
    // update leads and cons outside for loop
    
    
}
Thanks so much in advance! 
AnudeepAnudeep (Salesforce Developers) 
Hi Charni - Your code looks good. Here is a sample code that might help
 
trigger ConvertLead on Lead (before insert) {
    List<String> leadEmails = new List<String>();
    for(Lead lead:Trigger.new){
        leadEmails.add(lead.Email);
    }

    List<Contact> contacts = [
        SELECT 
            Id, Email 
        FROM 
            Contact
        WHERE 
            Email IN :leadEmails
    ];

    Set<String> contactEmails = new Set<String>();
    for(Contact contact:contacts){
        contactEmails.add(contact.Email);
    }

    for(Lead lead:Trigger.new){
        if(contactEmails.contains(lead.Email){
            lead.Status = 'Converted';
        }
    }
}

Let me know if this helps. It it does, please close this query by marking it as solved. It may help others in the community. Thank You!
Charni Wiggins 9Charni Wiggins 9
Hi Anudeep, 

Thanks for this.. would this set the contact Id on the lead record? Also, if I wanted to make further updates to the contact, how could I do that?

Thanks so much,

Charni
AnudeepAnudeep (Salesforce Developers) 
Charni - If you want to perform further updates to contact, you should do so like this. And no it doesn't set the contact Id and you would need to set it using lead.ContactId
 
if (lead.IsConverted) {
            // Assign the value from the Lead "Status" field to the Contact "Type" field
            Contact con = [SELECT Id FROM Contact WHERE Contact.Id = :lead.ConvertedContactId];
            con.Type__c = lead.Status;
            update con;
        }

Hope this helps
Charni Wiggins 9Charni Wiggins 9
Hi Anudeep,

Thank you for your suggestion. I am still struggling to figure out how to set ConvertedContactId, are you able to explain a bit further for me please?