You need to sign in to do that
Don't have an account?
Charni 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!
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!
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
Hope this helps
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?