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
JustinWilliams2381JustinWilliams2381 

Trying to set existing contactID based on email match during lead auto convert

I have created a simple auto-covert lead trigger that works.  I upgraded it to attach to an existing account ID if that account was identified on a custom lookup field on the lead.  Now I want to upgrade it again to add to an existing contact if the lead email matches one of the emails from a contact record related to the account that was identified on the lead.  Here is my code and I am trying to figure out how to reference the lead email and the contact email in a SOQL query.  I know I also need to add a chunck of code to check and see if my results are blank but I need to figure out this SOQL thing first.

 

trigger AutoConvertLead on Lead (After Update) {
    for (Lead lead : Trigger.new) {
        if (lead.isConverted == false && lead.Qualification_Score__c>=6) //to prevent recursion
        {
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId(lead.Id);
            
            String oppName =  lead.Name;
            lc.setOpportunityName(oppName);
            
            LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
            lc.setConvertedStatus(convertStatus.MasterLabel);
            
            //If the account lookup field is filled out then it will merge to that account instead of creating a new one.
            if (lead.Account__c != null)
            {
                String ExistingAccount = lead.Account__c;
                lc.setAccountID(ExistingAccount);
            }

            if (lead.email != null)
            {
                String CurrentEmail = lead.email;
                String ExistingContact = [SELECT Id FROM Contact WHERE email = CurrentEmail AND AccountID = ExistingAccount LIMIT 1];
                lc.setContactID(ExistingContact);
            }
            
            Database.LeadConvertResult lcr = Database.convertLead(lc);
            System.assert(lcr.isSuccess());
        }
    }
}

 

Sandeep001Sandeep001

You need to use bind variables in SOQL : (use currentemail and existing account with colon : in SOQL)

 

String ExistingContact = [SELECT Id FROM Contact WHERE email = :CurrentEmail AND AccountID = :ExistingAccount LIMIT 1];

 You can also refer - http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_variables.htm

Bhawani SharmaBhawani Sharma
Loop through all the lead records first and create a set of email Ids.

Now query all the contacts where email in this set like
Select id, email from Contact where Email iN: set
loop through all the records and create a map where key is email id and value is contact id.

Now again loop through the leads and do your processing. When you are processing your lead records, you can check if map contains that email address, if yes, then get contact Id and assign it into lead.