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
AtherisAtheris 

Lead convert functionality to exclude Deleted accounts

Currently when a new Lead is created and certain fields match with an existing Account, the Lead will be converted with the matching Account. 
However there needs to be a filter not to convert a fresh lead with an account that is set as Deleted (boolean field)

This criteria should probably go somwhere between this code piece. Does anyone have any ideas regarding this?

 
//All accounts that match the lead emails.
    List<Account> accounts = [SELECT Id 
                              , FirstName
                              , LastName
                              , PersonEmail
                              FROM Account 
                              WHERE PersonEmail IN: leadEmails];
    
    Map<String, Account> accountsByEmail = new Map<String, Account>();
    
    for(Account account : accounts)
    {
        accountsByEmail.Put(account.PersonEmail, account);
    }
    
    //All the leads that need to be converted into new accounts.
    List<Database.LeadConvert> leadsToConvert = new List<Database.LeadConvert>();
    
    //All the leads that match an existing account. These accounts will be updated and the lead set to converted.
    Set<Id> existingAccountsForLeads = new Set<Id>();
    
    for (Lead lead : newLeadsById.Values())
    {
        //to prevent recursion 
        if (lead.isConverted == false && lead.Email_already_in_the_system__c == false) 
        { 
            Database.LeadConvert leadConvert = new Database.LeadConvert();
            leadConvert.SetLeadId(lead.Id);
            leadConvert.SetDoNotCreateOpportunity(true);
            leadConvert.SetConvertedStatus(convertedLeadStatuses[0].MasterLabel);
            
            //Check whether an account already exists with this lead's email and name. If one already exists we do not need to convert 
            //to a new account as they are already a customer. We rather need to update the existing record.
            Account matchingAccount = accountsByEmail.Get(lead.Email);
            if (matchingAccount != null && matchingAccount.FirstName == lead.FirstName && matchingAccount.LastName == lead.LastName)
            {
                leadConvert.SetAccountId(matchingAccount.Id);
                existingAccountsForLeads.Add(matchingAccount.Id);
            }
            
            leadsToConvert.Add(leadConvert);
        }
    }


 
Best Answer chosen by Atheris
Abhishek BansalAbhishek Bansal
Hi,

I have modified your code.
Please replace your code with below code :

v
//All accounts that match the lead emails.
    List<Account> accounts = [SELECT Id 
                              , FirstName
                              , LastName
                              , PersonEmail,
                              Deleted__c
                              FROM Account 
                              WHERE PersonEmail IN: leadEmails];
    
    Map<String, Account> accountsByEmail = new Map<String, Account>();
    
    for(Account account : accounts)
    {
        accountsByEmail.Put(account.PersonEmail, account);
    }
    
    //All the leads that need to be converted into new accounts.
    List<Database.LeadConvert> leadsToConvert = new List<Database.LeadConvert>();
    
    //All the leads that match an existing account. These accounts will be updated and the lead set to converted.
    Set<Id> existingAccountsForLeads = new Set<Id>();
    
    for (Lead lead : newLeadsById.Values())
    {
        //to prevent recursion 
        if (lead.isConverted == false && lead.Email_already_in_the_system__c == false) 
        { 
            Database.LeadConvert leadConvert = new Database.LeadConvert();
            leadConvert.SetLeadId(lead.Id);
            leadConvert.SetDoNotCreateOpportunity(true);
            leadConvert.SetConvertedStatus(convertedLeadStatuses[0].MasterLabel);
            
            //Check whether an account already exists with this lead's email and name. If one already exists we do not need to convert 
            //to a new account as they are already a customer. We rather need to update the existing record.
            Account matchingAccount = accountsByEmail.Get(lead.Email);
            if (matchingAccount != null && matchingAccount.FirstName == lead.FirstName && matchingAccount.LastName == lead.LastName && matchingAccount.Deleted__c == false)
            {
                leadConvert.SetAccountId(matchingAccount.Id);
                existingAccountsForLeads.Add(matchingAccount.Id);
            }
            
            leadsToConvert.Add(leadConvert);
        }
    }

Please change Deleted__c with original API name of field DELETED on Account.

Let me know if you need more help on this.

Thanks,
Abhishek

All Answers

Abhishek BansalAbhishek Bansal
Hi,

I have modified your code.
Please replace your code with below code :

v
//All accounts that match the lead emails.
    List<Account> accounts = [SELECT Id 
                              , FirstName
                              , LastName
                              , PersonEmail,
                              Deleted__c
                              FROM Account 
                              WHERE PersonEmail IN: leadEmails];
    
    Map<String, Account> accountsByEmail = new Map<String, Account>();
    
    for(Account account : accounts)
    {
        accountsByEmail.Put(account.PersonEmail, account);
    }
    
    //All the leads that need to be converted into new accounts.
    List<Database.LeadConvert> leadsToConvert = new List<Database.LeadConvert>();
    
    //All the leads that match an existing account. These accounts will be updated and the lead set to converted.
    Set<Id> existingAccountsForLeads = new Set<Id>();
    
    for (Lead lead : newLeadsById.Values())
    {
        //to prevent recursion 
        if (lead.isConverted == false && lead.Email_already_in_the_system__c == false) 
        { 
            Database.LeadConvert leadConvert = new Database.LeadConvert();
            leadConvert.SetLeadId(lead.Id);
            leadConvert.SetDoNotCreateOpportunity(true);
            leadConvert.SetConvertedStatus(convertedLeadStatuses[0].MasterLabel);
            
            //Check whether an account already exists with this lead's email and name. If one already exists we do not need to convert 
            //to a new account as they are already a customer. We rather need to update the existing record.
            Account matchingAccount = accountsByEmail.Get(lead.Email);
            if (matchingAccount != null && matchingAccount.FirstName == lead.FirstName && matchingAccount.LastName == lead.LastName && matchingAccount.Deleted__c == false)
            {
                leadConvert.SetAccountId(matchingAccount.Id);
                existingAccountsForLeads.Add(matchingAccount.Id);
            }
            
            leadsToConvert.Add(leadConvert);
        }
    }

Please change Deleted__c with original API name of field DELETED on Account.

Let me know if you need more help on this.

Thanks,
Abhishek
This was selected as the best answer
AtherisAtheris
Thank you Abhishek. This did the trick!