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

Apex Code Coverage

We have an issue where a Unit Test keeps failing and only reaching 37% cover.


When I look at the trigger with 37% coverage and see where the issue lies, below is what I get where the error is in red. I'm not sure how to fix this, and I was hoping someone could please help me on what I need to do to get this coverage up because it's already in our production system, and we can't load any new coding in because the validation fails as a result of the error here.


Any help or direction on this would be GREATLY appreciated.


Thank you!


Code Coverage Help for this Page

LeadConversionTrigger (Code Covered: 37%)


 line source
 1  trigger LeadConversionTrigger on Lead (after update) {
 3   // loop over all leads being updated
 4   Integer i;
 5   for(i=0; i<; i++) {
 7   // check if this is a conversion
 8   if (Trigger.old[i].isConverted == false &&[i].isConverted == true) {
 10   // if a new opportunity was created
 11   if ([i].ConvertedOpportunityId != null) {
 13   // update the converted opportunity with some fields from the lead
 14   Opportunity opp = [Select o.Id, o.Amount, o.Note_Box__c from Opportunity o Where o.Id =[i].ConvertedOpportunityId];
 15   opp.Amount =[i].Amount__c;
 16   opp.Note_Box__c =[i].Note_Box__c;
 17   update opp;
 18   }
 19   }
 20   }




If this trigger is the problem, you cannot have much code in your SF instance.


Anyways, the problem is probably that your test code never enters this if-clause:


if (Trigger.old[i].isConverted == false &&[i].isConverted == true) {


So what you need to do is to create testdata with these characteristics and then update or insert or what ever the trigger might trigger on. 



HTH / Niklas