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
Danielle Pulley 10Danielle Pulley 10 

Failed Code Coverage only 74% needing 75%, what am I not doing?

I recently tried to push code from my sandbox to production and recieved an erro saying I only had 74% coverage and need 75%. Also, my trigger has 0% coverage. I am new to apex but can anyone help me understand why this is failing or how to correct it. Below is my trigger and class I created to convert Leads. 

Trigger: 
trigger LeadConvert on Lead (after insert,after update) {
//Bulkified
List<String> LeadNames = new List<String>{};
for(Lead myLead: Trigger.new){
 if((myLead.isconverted==false) && (myLead.LeadSource == 'Academy Registration'||
 myLead.LeadSource == 'User Registration' || myLead.LeadSource == 'Organization Registration')) {
Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(myLead.Id);
        lc.convertedStatus = 'Qualified';
        //Database.ConvertLead(lc,true);
        lc.setDoNotCreateOpportunity(true);
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
        }
        }
}


Class: 
public class LeadClass {
    public static void doConvert(Id leadId){ 
    Database.LeadConvert lc = new Database.LeadConvert();
    lc.setLeadId(leadId);

    Lead convertStatus = [SELECT Id,  LeadSource FROM Lead WHERE LeadSource = 'Academy Registration ' LIMIT 1];
    lc.setConvertedStatus(convertStatus.LeadSource);

    Database.LeadConvertResult lcr = Database.convertLead(lc);
    System.assert(lcr.isSuccess());
    }
}


All help is appreciated!

Thanks, 

Dani
Shukla YogeshShukla Yogesh

Hi Dani

I believe that there are issue in the test class. Can you try a similar kind of code?

@IsTest private class TEST_LeadConvert{


private static testMethod void myUnitTest() {

// create a Lead
Lead lead=new Lead(LastName='Doe',FirstName='John',Company='Test',Status='Inquiry');

insert lead;                

Database.LeadConvert lc = new database.LeadConvert();

//////////////////////////////////////////
lc.setLeadId(lead.id); //etc
//////////////////////////////////////////

Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());
}}
 

Thanks
Yogesh

 

Glyn Anderson 3Glyn Anderson 3
Dani,

You've posted your trigger and a utility class, but you don't seem to have a test class.  You won't get any code coverage without a test class.  Yogesh posted an example of a test class, and you could adapt that.  I would like to understand overall what you're trying to accomplish.  Once the code is ready, then we can create an appropriate test class.

Your trigger is great.  It is easy to understand what it does:  If a Lead is inserted/updated with one of those three LeadSource values, you convert it.  I'm having a harder time understanding the utility class.  The "doConvert" method will query the Lead whose Id is passed in - but only if its LeadSource is 'Academy Registration'; otherwise the method will throw an exception ("No rows to assign..." or something like that).  There doesn't seem to be any reason to query the LeadSource value when you're filtering on the LeadSource value - you know exactly what value you will get (if no exception is thrown).  So help me understand what you want that method to do, and I can help you improve it.

Another question - the trigger will automatically convert any Lead that has the 'Academy Registration' LeadSource when it is inserted or updated.  So what do you need the class for?  There won't be any unconverted Leads with that LeadSource value.

Eager to help,
Glyn
Glyn Anderson 3Glyn Anderson 3
BTW:  Here's my formatting of your trigger.  I find this indent style makes the code easy to read for me.  Some people don't like this style.  Style is a personal choice - I hope this example helps you think about your own style.

<pre>
trigger LeadConvert on Lead ( after insert, after update )
{
    List<String> LeadNames = new List<String>();
    for ( Lead myLead : Trigger.new )
    {
        if  (   !myLead.isConverted
            &&  (   myLead.LeadSource == 'Academy Registration'
                ||  myLead.LeadSource == 'User Registration'
                ||  myLead.LeadSource == 'Organization Registration'
                )
            )
        {
            Database.LeadConvert lc = new Database.LeadConvert();
            lc.setLeadId( myLead.Id );
            lc.convertedStatus = 'Qualified';
            lc.setDoNotCreateOpportunity( true );
            Database.LeadConvertResult lcr = Database.convertLead( lc );
        }
    }
}
</pre>
Glyn Anderson 3Glyn Anderson 3
@amahm, Since you got your answer from Yogesh, could you please delete your post?  It is irrelevent to this topic.  And then I will delete this post.  Thank you.
Danielle Pulley 10Danielle Pulley 10
Glyn, 

I created a trigger to automatically convert a lead when the Lead Source is Academy, User or Org. 

 

Yogesh, 

I tried your test class but there is an error for the status. I changed it to New but still having errors when deploying to production. 


Thanks,
Dani