You need to sign in to do that
Don't have an account?
raman123
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());
}
}
}
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());
}
}
}
All Answers
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