You need to sign in to do that
Don't have an account?
Charni Wiggins 9
Create a map of Lead Id, Contact Id?
I am creating a trigger which upon creation of a lead, searches for any contacts with a matching email, and if found - convert the lead, and set the ContactId on the lead. I am just having some trouble figuring out how to create a map, I think it should be <LeadId, ContactId> and then pass this to the Database.leadconvert class to set the contactId.
Forgive me for my code - admin trying to be a dev. Thank you for any help!
trigger convertLead on Lead (before insert) { Set<String> formLeads = new Set<String>(); // Loop through leads, store in set when FORM == TRUE for(Lead l : Trigger.New) { if(l.Form_Submission_ADMIN_USE_ONLY__c == True) { l.Description = 'Trigger ran'; formLeads.add(l.email); } } // List of contacts - Select contacts when email IN emailstocompare List<Contact> matchingCons = [SELECT Id, Email FROM Contact WHERE Email IN :formLeads]; // Add cons to set Set<Id> conIds = new Set<Id>(); // loop through contacts, add to set for(Contact cons:matchingCons) { conIds.add(cons.Id); } /* START Convert Lead Stuff */ LeadStatus convertStatus = [SELECT MasterLabel FROM LeadStatus WHERE IsConverted = true LIMIT 1]; List<Database.LeadConvert> leadsConvert = new List<Database.LeadConvert>(); /* END Convert Lead Stuff */ // if lead and contact match if(conIds.size()>0) { Map<Id, Set<Id>> leadtoConvert = new Map<Id, Set<Id>>(); // construct map lead id to contact id - set in SetContactId method for(Lead l : Trigger.New) { leadtoConvert.put(l.Id, conIds); // convert lead Database.LeadConvert lc = new Database.leadConvert(); lc.setLeadId(l.Id); lc.setConvertedStatus(convertStatus.Masterlabel); lc.SetContactId(leadtoConvert.get(l.Id)); database.LeadConvertResult lcr = Database.convertLead(lc); System.assert(lcr.isSuccess()); } } // loop through contacts, do update }
Map<String,Id> leadEmailToId = new Map<String,Id>();
Loop over leads like you're doing at the start and if they meet your criteria: leadEmailToId.put(l.Email, l.Id);
Then in your contact query to get all contacts with matching emails: [SELECT Id, Email FROM Contact WHERE Email IN :leadEmailToId.keySet()];
Then create the contact map:
Map<String,Id> contactEmailToId = new Map<String,Id>();
for(Contact con : matchingCons) {
contactEmailToId.put(con.Email, con.Id);
}
Then loop over the leadEmailToId list, since that contains the emails that met your criteria:
List<Database.LeadConvert> leadsToConvert = new List<Database.LeadConvert>(); // create a list so you aren't doing the conversions inside a loop
for (String email : leadEmailToId.keySet()) {
Id contactId = contactEmailToId.get(email);
if (String.isBlank(contactId)) continue; // don't convert, there was no matching email on a contact
Database.LeadConvert lc = new Database.leadConvert();
lc.setLeadId(leadEmailToId.get(email));
lc.setConvertedStatus(convertStatus.Masterlabel);
lc.setContactId(contactId);
leadsToConvert.add(lc);
}
Database.convertLead(leadsToConvert);
NOTE: this assumes that emails are unique on Leads and Contacts