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
Francisco Corona 7Francisco Corona 7 

I need help with making my following apex class 75% code coverage, right now it is at 68%. Any help is appreciated!

Public class AutoConvertLeads
{
@InvocableMethod
public static void LeadAssign(List<Id> LeadIds)
{
LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();

List<Lead> myLeads = [Select id, name, company, Matched_Account_ID__c from Lead where id IN:LeadIds];
List<String> MatchedAccount = New List<String>();

for (Lead l:myLeads){
MatchedAccount.add(l.Matched_Account_ID__c);
}

List<Account> matchAccount = [Select id, Name from account where id IN:MatchedAccount];

Integer i=0;
for(Lead currentlead: myLeads){
for (Account a:matchAccount){
if (currentlead.Matched_Account_ID__c == a.id){
Database.LeadConvert Leadconvert = new Database.LeadConvert();
Leadconvert.setLeadId(currentlead.id);
Leadconvert.setAccountId(a.id);
Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
Leadconvert.setDoNotCreateOpportunity(TRUE); //Remove this line if you want to create an opportunity from Lead Conversion
MassLeadconvert.add(Leadconvert);
i++;
Break;
}
}
If (i==0)
{
Database.LeadConvert Leadconvert = new Database.LeadConvert();
Leadconvert.setLeadId(currentlead.id);
Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
Leadconvert.setDoNotCreateOpportunity(TRUE); //Remove this line if you want to create an opportunity from Lead Conversion
MassLeadconvert.add(Leadconvert);
}
i=0;
}

if (!MassLeadconvert.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
}
}
}
Francisco Corona 7Francisco Corona 7
Also want to note that i have a test class for this. Does a regular apex class need a test class?
Alok Singh 140Alok Singh 140
Yes you need to cover your code in your test class.
Alok Singh 140Alok Singh 140
just cover your code with all the scenario like If conditions it will inrease your code coverage.