You need to sign in to do that
Don't have an account?
Tanmay Sahai
Help with Test Class and Code bulkification
Hello All,
I have created an Apex class with invocable method and using it in a Process builder to automate my lead convrsion. The code is working but I need help with 2 things:
1. My code is creating duplicate accounts. Can someone help with the de-duplication of the Account. Basically what I am looing for is that if there is already an account, the lead should only be converted to Contact and if there is no Account, then it should create both Account and Contact.
2. I need help with writing a Test Class so that I can deploy it to Production.
Do I need to make sure that both my Apex class and Test Class have a 75% code coverage or more.
Below is my Apex Class code:
Public class AutoConvertLeads
{
@InvocableMethod
public static void LeadAssign(List<Id> LeadIds)
{
LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 100];
List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
for(id currentlead: LeadIds){
Database.LeadConvert Leadconvert = new Database.LeadConvert();
Leadconvert.setLeadId(currentlead);
Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
Leadconvert.setDoNotCreateOpportunity(TRUE);
MassLeadconvert.add(Leadconvert);
}
if (!MassLeadconvert.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
}
}
}
Any quick help will be appreciated. Thanks in advance!
I have created an Apex class with invocable method and using it in a Process builder to automate my lead convrsion. The code is working but I need help with 2 things:
1. My code is creating duplicate accounts. Can someone help with the de-duplication of the Account. Basically what I am looing for is that if there is already an account, the lead should only be converted to Contact and if there is no Account, then it should create both Account and Contact.
2. I need help with writing a Test Class so that I can deploy it to Production.
Do I need to make sure that both my Apex class and Test Class have a 75% code coverage or more.
Below is my Apex Class code:
Public class AutoConvertLeads
{
@InvocableMethod
public static void LeadAssign(List<Id> LeadIds)
{
LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 100];
List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
for(id currentlead: LeadIds){
Database.LeadConvert Leadconvert = new Database.LeadConvert();
Leadconvert.setLeadId(currentlead);
Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
Leadconvert.setDoNotCreateOpportunity(TRUE);
MassLeadconvert.add(Leadconvert);
}
if (!MassLeadconvert.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
}
}
}
Any quick help will be appreciated. Thanks in advance!
what do you mean by " if there is already an account, the lead should only be converted to Contact and if there is no Account "?
From what I understand from your question, if the Lead is already associated with an account, then you want the lead to be under the related account if not then convert it into a new account and Lead.
IF this is right, then
try this code for the Lead Conversion but still, you will need a field that connects the account to lead, see if you already have one, for the time being, we will call it 'accountid__c'
public static map< id,id> mapAccount = new map<id,id>();
for(Account a : [select id from Account])
{
mapAccountount.put(a.id, a.id);
}
list<Database.LeadConvert> MassLeadconvert = new list<Database.LeadConvert>();
for(id currentlead: LeadIds)
{
if(mapAccount.get(currentlead.accountid__c) != NULL)
{
Database.LeadConvert Leadconvert = new Database.LeadConvert();
Leadconvert.setLeadId(currentlead);
Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
Leadconvert.setDoNotCreateOpportunity(TRUE);
Leadconvert.setAccountId(mapAccount.get(currentlead.accountid__c));
MassLeadconvert.add(Leadconvert);
}
else
{
Database.LeadConvert Leadconvert = new Database.LeadConvert();
Leadconvert.setLeadId(currentlead);
Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
Leadconvert.setDoNotCreateOpportunity(TRUE);
MassLeadconvert.add(Leadconvert);
}
}
if (!MassLeadconvert.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
}
All Answers
what do you mean by " if there is already an account, the lead should only be converted to Contact and if there is no Account "?
From what I understand from your question, if the Lead is already associated with an account, then you want the lead to be under the related account if not then convert it into a new account and Lead.
IF this is right, then
try this code for the Lead Conversion but still, you will need a field that connects the account to lead, see if you already have one, for the time being, we will call it 'accountid__c'
public static map< id,id> mapAccount = new map<id,id>();
for(Account a : [select id from Account])
{
mapAccountount.put(a.id, a.id);
}
list<Database.LeadConvert> MassLeadconvert = new list<Database.LeadConvert>();
for(id currentlead: LeadIds)
{
if(mapAccount.get(currentlead.accountid__c) != NULL)
{
Database.LeadConvert Leadconvert = new Database.LeadConvert();
Leadconvert.setLeadId(currentlead);
Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
Leadconvert.setDoNotCreateOpportunity(TRUE);
Leadconvert.setAccountId(mapAccount.get(currentlead.accountid__c));
MassLeadconvert.add(Leadconvert);
}
else
{
Database.LeadConvert Leadconvert = new Database.LeadConvert();
Leadconvert.setLeadId(currentlead);
Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
Leadconvert.setDoNotCreateOpportunity(TRUE);
MassLeadconvert.add(Leadconvert);
}
}
if (!MassLeadconvert.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
}
Thanks for your quick response. Well this is exactly my requirement was. But I have few questions:
1. Will this work for bulk conversion( records more than 200)
2. How can I use this code for invoking??
3. How to write the Test class for it to complete my code coverage and deployment??
Await your response. Thanks!
for invoking the code there are multiple ways, some of them are
1. you can append it to a trigger on Lead if you have one since you are using a LeadId list to loop, I am assuming you might have one
2. you can run it as a one time batch or can schedule the class to run at a specific time
3 u can also use a process builder as in your question with @InvocableMethod
Test class for the code depends on where you append or on the overall workflow. however, you can refer this thread as a reference https://salesforce.stackexchange.com/questions/36823/help-creating-a-test-class-for-lead-conversion-trigger
Thanks again for your response. Your code is throwing error.
Can you help me bulkify the original code that I shared. It is currently at 0% code coverage. I was able to create a Test Class with 100% code covrage and it was successfully deployed.
Await your response. Thanks!
As per our lead conversion process, a lead is converted to An Account (the name of the company) and contact(name of the lead) for the first time. Then if we want to create a new contact for the existing account, we again create a new lead and select the existing Account as company and convert that lead into contact and the contact gets attached to the Account.
I have made few changes in your code to make it work as per my requirements and it is converting the lead but it is still creating duplicate accounts.
Can you please help me with the code to fulfill my requirement of preventing duplicate account creation.
I will be highly obliged. Await your response!
Thanks!