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 

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

}
 

 

GCW LabsGCW Labs
You need two maps, of email to id:

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
GCW LabsGCW Labs
Also note that you can only run Database.convertLeads on a list of 100, and triggers run on 200 records. So if the list is longer than 100, you'll need to split it into two and run Databsae.convertLead() twice