+ Start a Discussion
raman123raman123 

my code is not working as per my requirement

When a new person account is created, check if there are existing unconverted leads with the same email address.
If any lead is found, convert lead using the same account which is creating it. If there are multiple leads found, first merge to latest modified lead. (Merge can only happen for max 3 leads as per Salesforce Limit. If we have more duplicates lead, we might have to consider using batch). 
If no leads found, do nothing.

code
public class AccountTriggerHandler {
    public static void existingunconvertedleads (List<Account> lstInsertedAccount){
        map<string,Account> mapOfAccount = new map<string,Account>();
        
        // Check to see if Email already exists
        for (Account a : lstInsertedAccount) {
            if(a.IsPersonAccount){
                if(String.isnotBlank(a.PersonEmail)){
                    mapOfAccount.put(a.PersonEmail,a);
                }
            }
        }
        Map<string,List<Lead>> mapOfLeadWithEmail = new Map<string,List<Lead>>();
        for(lead objld :[select id,email from lead where IsConverted = false And Email in: mapOfAccount.keySet()]){
            if(mapOfLeadWithEmail.containsKey(objld.Email)){
                List<lead> ldlist =  mapOfLeadWithEmail.get(objld.Email);
                ldlist.add(objld);
                mapOfLeadWithEmail.put(objld.Email,ldlist);
            } else{
                mapOfLeadWithEmail.put(objld.Email, new list<lead>{objld});   
            }
            
        }
        
        
        for(string strEmail: mapofLeadWithEmail.keyset()){
            if(mapofLeadWithEmail.get(strEmail).size() > 1){
              list<lead> ls = [select id from lead where id =:mapOfLeadWithEmail.keyset()];
                
                
                LeadStatus CLeadStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true];
                List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
                for(id currentlead: mapOfAccount.keyset()){
                    Database.LeadConvert Leadconvert = new Database.LeadConvert();
                    Leadconvert.setLeadId(currentlead);                
                    Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                    MassLeadconvert.add(Leadconvert);
                    if (!MassLeadconvert.isEmpty()) {
                        List<Database.LeadConvertResult > lcr = Database.convertLead(MassLeadconvert);
                    }
                } 
            }
               
            
            system.debug('strEmail ::: '+strEmail);
            system.debug('Lead size ::: '+mapofLeadWithEmail.get(strEmail).size());
        }
    }
}
Best Answer chosen by raman123
Tad Aalgaard 3Tad Aalgaard 3
I see a lot of room for improvement in your code.  But I'll concentrate on one area for now.
 
for(string strEmail: mapofLeadWithEmail.keyset()){
            if(mapofLeadWithEmail.get(strEmail).size() > 1){
              list<lead> ls = [select id from lead where id =:mapOfLeadWithEmail.keyset()]; // don't put SOQL inside of a loop
                
                
                LeadStatus CLeadStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true];
                List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
                for(id currentlead: mapOfAccount.keyset()){
                    Database.LeadConvert Leadconvert = new Database.LeadConvert();
                    Leadconvert.setLeadId(currentlead);                
                    Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                    MassLeadconvert.add(Leadconvert);
                    if (!MassLeadconvert.isEmpty()) {
                        List<Database.LeadConvertResult > lcr = Database.convertLead(MassLeadconvert); // this is trying to convert every Leadconvert record more than once since you are putting it into a list calling it over an over again on the same records in the List, you should run this command outside of your for loops at the end once you have completed collection of the list. 
                    }
                } 
            }

 

All Answers

Tad Aalgaard 3Tad Aalgaard 3
I see a lot of room for improvement in your code.  But I'll concentrate on one area for now.
 
for(string strEmail: mapofLeadWithEmail.keyset()){
            if(mapofLeadWithEmail.get(strEmail).size() > 1){
              list<lead> ls = [select id from lead where id =:mapOfLeadWithEmail.keyset()]; // don't put SOQL inside of a loop
                
                
                LeadStatus CLeadStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true];
                List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
                for(id currentlead: mapOfAccount.keyset()){
                    Database.LeadConvert Leadconvert = new Database.LeadConvert();
                    Leadconvert.setLeadId(currentlead);                
                    Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                    MassLeadconvert.add(Leadconvert);
                    if (!MassLeadconvert.isEmpty()) {
                        List<Database.LeadConvertResult > lcr = Database.convertLead(MassLeadconvert); // this is trying to convert every Leadconvert record more than once since you are putting it into a list calling it over an over again on the same records in the List, you should run this command outside of your for loops at the end once you have completed collection of the list. 
                    }
                } 
            }

 
This was selected as the best answer
raman123raman123
how can i write test class for it @Tad
Tad Aalgaard 3Tad Aalgaard 3
Looking at your previous posts on your profile you have asked others how to write test classes before.  From what I can tell you are hoping that I will write it for you.  I see way too many people asking others how to write test classes on this forum in hopes that someone else will do their work for them.  I am not going to do that.  Instead, I am going to suggest that you read Salesforce documentation about writing test classes and do some Trailheads and learn how to do this on your own.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm (https://trailhead.salesforce.com/en/content/learn/modules/apex_testing)

https://trailhead.salesforce.com/en/content/learn/modules/apex_testing