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
Daniel GageDaniel Gage 

Only 41 percent Coverage on Lead Convert Trigger.. help?

I am new to Apex development, and am trying to figure out how to get past the !lead.isConverted part of my Trigger in the Test case.  The test case is only making it to the IF Statement of the Trigger.

Thank you in advance for your help.
trigger LeadConvertVerified on Lead (after Update) {
string convertStatus = 'Closed - Converted';
List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
for (Lead lead: Trigger.new) {
   if (!lead.isConverted && lead.Verified__c == True){
    Database.LeadConvert lc = new Database.LeadConvert();
    lc.setLeadId(lead.Id);
    lc.setAccountId(lead.Organization__c);
    lc.setDoNotCreateOpportunity(TRUE);
    lc.ConvertedStatus= convertStatus;
    leadConverts.add(lc);
        }
    }
if (!leadConverts.isEmpty()) {
List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
}
}

@istest
private class LeadConversionTriggerTestClass {

Static testMethod void LeadConvertTriggerTestClass() {
//Test Against Verified Lead
  Lead l1 = New Lead();
   l1.IsConverted = False;
   l1.FirstName = 'TestOne';
  l1.LastName = 'Testing';
  l1.Organization__c = '001L000000TSsoD';
  l1.Email = 'testone@testing.com';
  l1.Company = 'Individual';
  l1.Verified__C = True;
  insert l1;
    Database.LeadConvert lc = new database.LeadConvert();
         lc.setLeadId(l1.Id);
         LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
         lc.setConvertedStatus(convertStatus.MasterLabel);
         Database.LeadConvertResult lcr = Database.convertLead(lc);
    
//Test Against Un Verified Lead
Lead l2 = New Lead();
    l2.IsConverted = False;
  l2.FirstName = 'TestOne';
  l2.LastName = 'Testing';
  l2.Organization__c = '001L000000TSsoD';
  l2.Email = 'testone@testing.com';
  l2.Company = 'Individual';
  l2.Verified__C = False;
  insert l2;    
    Database.LeadConvert lc2 = new database.LeadConvert();
         lc.setLeadId(l2.Id);
         LeadStatus convertStatus2 = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
         lc.setConvertedStatus(convertStatus2.MasterLabel);
         Database.LeadConvertResult lcr2 = Database.convertLead(lc);
  }
  }
Best Answer chosen by Daniel Gage
Roy LuoRoy Luo
Your trigger is on Lead for After Update event and your unit test doesn't update on Lead, thus your test should never hit the Lead After update trigger. 

I don't know your business needs, just from codind apects, you could either add after insert event to the trigger, or do update on a Lead.

trigger LeadConvertVerified on Lead (after Insert, after Update) {}

OR 
 
Lead l1 = New Lead();
   l1.IsConverted = False;
   l1.FirstName = 'TestOne';
  l1.LastName = 'Testing';
  l1.Organization__c = '001L000000TSsoD';
  l1.Email = 'testone@testing.com';
  l1.Company = 'Individual';
  l1.Verified__C = True;
  insert l1;

l1.LastName='Testing Update';
update l1;

BTW, Advance Apex Programming by Dan Appleman is a good book.

All Answers

pooja kadampooja kadam
In trigger you are mapping convertedstatus as string convertStatus = 'Closed - Converted', which is a hardcoded one and in the test class you are querying and mapping.
Daniel GageDaniel Gage

Thanks for the quick reply. It must be too late for my brain to work right though. I have changed (at seperate times) Both the way it is done in the trigger and the way it is done in the test case.. and came up failing on both tries.

When I changed the Trigger to match the Test Case (for the convert Status)... I got this "Methods defined as TestMethod do not support Web service callouts, test skipped"

When I changed the test case to match the trigger, it just failed in the same manner.

Thanks for your help. I'll try again in the morning.

Roy LuoRoy Luo
Your trigger is on Lead for After Update event and your unit test doesn't update on Lead, thus your test should never hit the Lead After update trigger. 

I don't know your business needs, just from codind apects, you could either add after insert event to the trigger, or do update on a Lead.

trigger LeadConvertVerified on Lead (after Insert, after Update) {}

OR 
 
Lead l1 = New Lead();
   l1.IsConverted = False;
   l1.FirstName = 'TestOne';
  l1.LastName = 'Testing';
  l1.Organization__c = '001L000000TSsoD';
  l1.Email = 'testone@testing.com';
  l1.Company = 'Individual';
  l1.Verified__C = True;
  insert l1;

l1.LastName='Testing Update';
update l1;

BTW, Advance Apex Programming by Dan Appleman is a good book.
This was selected as the best answer
Daniel GageDaniel Gage

Roy - Thanks. You are being very helpful tonight. Sadly, at this time I can't do an after insert on that Trigger because there is another trigger in place (from an installed package) that is generating an infinite loop. We are working through whether or not they will need this package anymore with the new code going into place.... so at that point I'll be able to add the after insert.

And..... your comment makes complete sense. I'll give it a try. Thanks again.
 

Daniel GageDaniel Gage

Roy - Thank you for your help. And for the book name. I will absolutely purchase it. Looks like I'll be getting a lot of Salesforce work in in the future.

Btw  - THis is up to 100 percent coverage. Thank you very very much.