Reply
Regular Contributor
SFDC-DMG
Posts: 19
0

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) {
 2  
 3   // loop over all leads being updated
 4   Integer i;
 5   for(i=0; i<Trigger.new.size(); i++) {
 6  
 7   // check if this is a conversion
 8   if (Trigger.old[i].isConverted == false && Trigger.new[i].isConverted == true) {
 9  
 10   // if a new opportunity was created
 11   if (Trigger.new[i].ConvertedOpportunityId != null) {
 12  
 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 = :Trigger.new[i].ConvertedOpportunityId];
 15   opp.Amount = Trigger.new[i].Amount__c;
 16   opp.Note_Box__c = Trigger.new[i].Note_Box__c;
 17   update opp;
 18   }
 19   }
 20   }
 21

  }

Regular Contributor
nickwick76
Posts: 93
0

Re: Apex Code Coverage

Hi, 

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 && Trigger.new[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

Force.com Certified Developer, Advanced Administrator, Sales Cloud Consultant and Service Cloud Consultant