+ Start a Discussion
Swami DeshanelSwami Deshanel 

Mass Convert Trigger Test Class Code Coverage

I have a trigger for converting leads but I can not get the code coverage past 26%.  Please Help

Trigger:
trigger AutoConvert on Lead (after update) {
    list<Lead> LeadsToConvert = new list<Lead>();
    for(Lead myLead: Trigger.new){
        if(!myLead.isConverted && myLead.status=='Consult to be Scheduled' )
            LeadsToConvert.add(myLead);
    }

    list<Database.LeadConvert> leadConverts = new list<Database.LeadConvert>();
    for(Lead myLead : LeadsToConvert){
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(myLead.Id);
        lc.convertedStatus = 'Consult to be Scheduled';
        //Database.ConvertLead(lc,true);
        lc.setDoNotCreateOpportunity(False);
        leadConverts.add(lc);
    }

    if(!leadConverts.isEmpty()){
        for(Integer i = 0; i <= leadConverts.size()/100 ; i++){
            list<Database.LeadConvert> tempList = new list<Database.LeadConvert>();
            Integer startIndex = i*100;
            Integer endIndex = ((startIndex+100) < leadConverts.size()) ? startIndex+100: leadConverts.size();
            for(Integer j=startIndex;j<endIndex;j++){
                tempList.add(leadConverts[j]);
            }
            Database.LeadConvertResult[] lcrList = Database.convertLead(tempList, false);
            for(Database.LeadConvertResult lcr : lcrList)
                System.assert(lcr.isSuccess());
        }
    }
}
Best Answer chosen by Swami Deshanel
Sumitkumar_ShingaviSumitkumar_Shingavi
You code should be
static testMethod void convertLead() {

	test.startTest();
	
	//Create Leads for Bulk testing
	List<Lead> lLeads = new List<Lead>();	
	for(Integer i = 0; i<150; i++) {		
		Lead lead = new Lead(
			FirstName='Trigger' + i, LastName='Test' + i, Company='Trigger Test' + i, Status='New');
		lLeads.add(lead);
	}
	insert lLeads;
	
	//Update status of all Leads to kick trigger on which will convert them
	for(Lead ld : lLeads) {
		ld.Status = 'Consult to be Scheduled';
	}
	update lLeads;
	
	test.stopTest();
}
Also, you might need to twick your entry criteria in that trigger like
for(Lead myLead: Trigger.new){
	if((!myLead.isConverted)
	&& myLead.status != Trigger.oldMap.get(myLead.Id).Status
	&& myLead.status == 'Consult to be Scheduled') {
		LeadsToConvert.add(myLead);
	}
}
Hope this helps you!

All Answers

Sumitkumar_ShingaviSumitkumar_Shingavi
You might need to create Leads in a for loop during writing your TC as your code need to have few hundreds of leads. It will help if you more focus on uncovered blocks really.
Swami DeshanelSwami Deshanel
Thank you for you quick reply.  Here is my test class.  I am new to writing trtiggers and I dont really understand how to create leads in a for loop.  Here is my test class

@isTest
public class TestAutoLeadConvert{
    static testMethod void convertLead(){
        test.startTest();
        Lead lead = new Lead();
        lead.FirstName='Trigger1';
        lead.LastName='Test1';
        lead.Company='Trigger Test1';
        lead.isConverted = false;
        lead.Status='Consult to be Scheduled';
        
        insert lead;
        
        System.debug('Created and inserted lead');
        
         Database.LeadConvert lc = new database.LeadConvert();
     lc.setLeadId(lead.Id);

     LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
     lc.setConvertedStatus(convertStatus.MasterLabel);
     Database.LeadConvertResult lcr = Database.convertLead(lc);

     // Make sure conversion was successful
     System.assert(lcr.isSuccess());

     test.stopTest();
        
        
            
    }
}
Roy LuoRoy Luo
Your trigger is on Lead After Update event, but your unit test doesn't have any code to update Lead.

Try this:

static testMethod void convertLead(){
        test.startTest();
        Lead lead = new Lead();
        lead.FirstName='Trigger1';
        lead.LastName='Test1';
        lead.Company='Trigger Test1';
        lead.isConverted = false;
        lead.Status='Consult to be Scheduled';
        
        insert lead;
        
      lead dbItem = [SELECT Id, LastName FROM Lead WHERE Id =:lead.Id];
    dbItem.LastName = 'Doe';
      update dbItem;


     test.stopTest();
        
        
            
    }

 
Sumitkumar_ShingaviSumitkumar_Shingavi
You code should be
static testMethod void convertLead() {

	test.startTest();
	
	//Create Leads for Bulk testing
	List<Lead> lLeads = new List<Lead>();	
	for(Integer i = 0; i<150; i++) {		
		Lead lead = new Lead(
			FirstName='Trigger' + i, LastName='Test' + i, Company='Trigger Test' + i, Status='New');
		lLeads.add(lead);
	}
	insert lLeads;
	
	//Update status of all Leads to kick trigger on which will convert them
	for(Lead ld : lLeads) {
		ld.Status = 'Consult to be Scheduled';
	}
	update lLeads;
	
	test.stopTest();
}
Also, you might need to twick your entry criteria in that trigger like
for(Lead myLead: Trigger.new){
	if((!myLead.isConverted)
	&& myLead.status != Trigger.oldMap.get(myLead.Id).Status
	&& myLead.status == 'Consult to be Scheduled') {
		LeadsToConvert.add(myLead);
	}
}
Hope this helps you!
This was selected as the best answer
Swami DeshanelSwami Deshanel
Awesome!  Thanks