+ Start a Discussion
sudhIr NarayansudhIr Narayan 

Improve code coverage in my trigger

Hi,

 Please suggest me am find challenge in improving performance in code coverage. Please advice me if this code is correct or not how to modify to get more code coverage. 

 Below code is working perfect only issue is with code coverage. 
 
trigger NewDiscountProgramUpdate on Opportunity (Before Update) 
{
// ******** Trigger Created By Sudhir ************  
// Loop through the incoming records

  list<opportunity> opp = new list<opportunity>();


for (Opportunity o : Trigger.new) {       
    
        
   // Check for Deal Reg Not Null
 if ( o.FORCE_NSP__c == NULL && o.discount_program__c <> 'NSP' && o.Deal_Registration__c <> NULL ) 
   {
     if ( o.Partner_Driven__c == 'Yes'  && o.Partner_Led__c == 'No' && o.K_12__c == 'No'  ) 
      {
        o.discount_program__c = 'DEALREG/PI';
        o.Abbv_Discount_Program__c = 'DR/PI';        
      }
     else if ( o.Partner_Driven__c == 'No'  && o.Partner_Led__c == 'Yes' && o.K_12__c == 'No'  ) 
     {
        o.discount_program__c = 'DEALREG/PL';
        o.Abbv_Discount_Program__c = 'DR/PL';    
     }
     else if ( o.Partner_Driven__c == 'Yes'  && o.Partner_Led__c == 'Yes' && o.K_12__c == 'No'  ) 
     {
       o.discount_program__c = 'DEALREG/PI/PL';
       o.Abbv_Discount_Program__c = 'DR/PI/PL';  
     }
     else if ( o.Partner_Driven__c == 'No'  && o.Partner_Led__c == 'No' && o.K_12__c == 'Yes' ) 
     {
       o.discount_program__c = 'DEALREG/K-12';
       o.Abbv_Discount_Program__c = 'DR/K12';    
     }
     else if ( o.Partner_Driven__c == 'Yes'  && o.Partner_Led__c == 'No' && o.K_12__c == 'Yes' )
     {
       o.discount_program__c = 'DEALREG/PI/K-12';
       o.Abbv_Discount_Program__c = 'DR/PI/K12';    
     }
     else if (   o.Partner_Driven__c == 'No'  && o.Partner_Led__c == 'Yes' && o.K_12__c == 'Yes'  )
     {
       o.discount_program__c = 'DEALREG/PL/K-12';
       o.Abbv_Discount_Program__c = 'DR/PL/K12';   
     }
     else if ( o.Partner_Driven__c == 'Yes'  && o.Partner_Led__c == 'Yes' && o.K_12__c == 'Yes' )
     {
       o.discount_program__c = 'DEALREG/PI/PL/K-12';
       o.Abbv_Discount_Program__c = 'DR/PI/PL/K12';   
     }
    else  
     {
     o.discount_program__c = 'DEALREG';
     o.Abbv_Discount_Program__c = 'DR';     
     } 
    }
     opp.add(o);
 }   
     
 
}


Please advice me also how to write test class for above trigger I wrote a test class which is giving only 14% code coverage. 

Thanks

Sudhir

SaranSaran
Hi Try this

@isTest
public class TaskTest
{
public static testmethod void validating ()
{
// Add all the required field in the opportunity.
opportunity opp = new opportunity();
opp.Name = 'TestOppty';
opp.FORCE_NSP__c = 'No';
opp.Partner_Led__c = 'No';
opp.K_12__c ='No';
insert opp

opportunity oppty = new opportunity([select id, discount_program__c, Abbv_Discount_Program__c from Opportunity where id =: opp.id])
System.assertEquals(oppty.discount_program__c, ‘DEALREG’);
System.assertEquals(oppty.Abbv_Discount_Program__c, ‘DR’);
}
}

Since there is lot of if condition, Instead of satisfying all the if condition you insert a record which disobey all if conditions so your code may be covered.

Hope this helped!!
Vatsal KothariVatsal Kothari
Hi Sudhir,
Can you share your test class?
Vatsal KothariVatsal Kothari
Hi Sudhir,
You can refer below code:
@istest
public class NewDiscountProgramUpdateTest{

	public static testmethod void testOpportunityUpdate ()
	{
		Account acc = new Account();
		acc.Name = 'test account';
		insert acc;
		
		Opportunity opp = new Opportunity();
		opp.Name = 'test Opportunity';
		opp.AccountId = acc.Id;		
		Opp.StageName='Closed Won';
		Opp.CloseDate = System.Today();
		insert opp;
		
		opp.Partner_Driven__c = 'Yes';
		opp.Partner_Led__c = 'No';
		opp.K_12__c = 'No';
		update opp;

		opp.Partner_Driven__c = 'No';
		opp.Partner_Led__c = 'Yes';
		opp.K_12__c = 'No';
		update opp;
		
		opp.Partner_Driven__c = 'Yes';
		opp.Partner_Led__c = 'Yes';
		opp.K_12__c = 'No';
		update opp;
		
		opp.Partner_Driven__c = 'No';
		opp.Partner_Led__c = 'No';
		opp.K_12__c = 'Yes';
		update opp;
		
		opp.Partner_Driven__c = 'Yes';
		opp.Partner_Led__c = 'No';
		opp.K_12__c = 'Yes';
		update opp;
		
		opp.Partner_Driven__c = 'No';
		opp.Partner_Led__c = 'Yes';
		opp.K_12__c = 'Yes';
		update opp;
		
		opp.Partner_Driven__c = 'Yes';
		opp.Partner_Led__c = 'Yes';
		opp.K_12__c = 'Yes';
		update opp;
	}
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal
sudhIr NarayansudhIr Narayan
Hi 

Please find my complete trigger I tried above test class to create i get only 23% Please adivce me how to improve
trigger NewDiscountProgramUpdate on Opportunity (before update) 
{
// ******** Trigger Created By Sudhir ************  
// Loop through the incoming records
for (Opportunity o : Trigger.new) {       
    
        
   // Check for Deal Reg Not Null
 if ( o.FORCE_NSP__c == NULL && o.discount_program__c <> 'NSP' && o.Deal_Registration__c <> NULL ) 
   {
     if ( o.Partner_Driven__c == 'Yes'  && o.Partner_Led__c == 'No' && o.K_12__c == 'No'  ) 
      {
        o.discount_program__c = 'DEALREG/PI';
        o.Abbv_Discount_Program__c = 'DR/PI';        
      }
     else if ( o.Partner_Driven__c == 'No'  && o.Partner_Led__c == 'Yes' && o.K_12__c == 'No'  ) 
     {
        o.discount_program__c = 'DEALREG/PL';
        o.Abbv_Discount_Program__c = 'DR/PL';    
     }
     else if ( o.Partner_Driven__c == 'Yes'  && o.Partner_Led__c == 'Yes' && o.K_12__c == 'No'  ) 
     {
       o.discount_program__c = 'DEALREG/PI/PL';
       o.Abbv_Discount_Program__c = 'DR/PI/PL';  
     }
     else if ( o.Partner_Driven__c == 'No'  && o.Partner_Led__c == 'No' && o.K_12__c == 'Yes' ) 
     {
       o.discount_program__c = 'DEALREG/K-12';
       o.Abbv_Discount_Program__c = 'DR/K12';    
     }
     else if ( o.Partner_Driven__c == 'Yes'  && o.Partner_Led__c == 'No' && o.K_12__c == 'Yes' )
     {
       o.discount_program__c = 'DEALREG/PI/K-12';
       o.Abbv_Discount_Program__c = 'DR/PI/K12';    
     }
     else if (   o.Partner_Driven__c == 'No'  && o.Partner_Led__c == 'Yes' && o.K_12__c == 'Yes'  )
     {
       o.discount_program__c = 'DEALREG/PL/K-12';
       o.Abbv_Discount_Program__c = 'DR/PL/K12';   
     }
     else if ( o.Partner_Driven__c == 'Yes'  && o.Partner_Led__c == 'Yes' && o.K_12__c == 'Yes' )
     {
       o.discount_program__c = 'DEALREG/PI/PL/K-12';
       o.Abbv_Discount_Program__c = 'DR/PI/PL/K12';   
     }
    else  
     {
     o.discount_program__c = 'DEALREG';
     o.Abbv_Discount_Program__c = 'DR';     
     } 
    }
    
    //Check for Deal Reg is NULL
  else if ( o.FORCE_NSP__c == NULL && o.Deal_Registration__c == NULL ) 
   {
     // This Code is used to set default values to pick list in renewal section 
      if ( o.Renewal_Opportunity__c == NULL && o.Renewal_Incumbant_Reseller__c == NULL && o.Renewal_K_12__c == NULL )
      {
         o.Renewal_Opportunity__c = 'No';
         o.Renewal_Incumbant_Reseller__c = 'No';
         o.Renewal_K_12__c = 'None';
       }       
     else if ( o.Renewal_Opportunity__c == 'Yes' && o.Renewal_Incumbant_Reseller__c == 'No' && o.Renewal_K_12__c == 'None' && o.Partner_Led__c == 'No'  ) 
     {
       o.discount_program__c = 'RENEWALS';
       o.Abbv_Discount_Program__c = 'RENEW';
     } 
     else if ( o.Renewal_Opportunity__c == 'Yes' && o.Renewal_Incumbant_Reseller__c == 'No' && o.Renewal_K_12__c == 'No' && o.Partner_Led__c == 'No'  ) 
     {
       o.discount_program__c = 'RENEWALS';
       o.Abbv_Discount_Program__c = 'RENEW';
     }
     else if ( o.Renewal_Opportunity__c == 'Yes'  && o.Renewal_Incumbant_Reseller__c == 'Yes' && o.Renewal_K_12__c == 'None' && o.Partner_Led__c == 'No'  ) 
     {
       o.discount_program__c = 'RENEWALS/INCUMBENT';
       o.Abbv_Discount_Program__c = 'RENEW/INC';
     }
     else if ( o.Renewal_Opportunity__c == 'Yes'  && o.Renewal_Incumbant_Reseller__c == 'Yes' && o.Renewal_K_12__c == 'No' && o.Partner_Led__c == 'No'  ) 
     {
       o.discount_program__c = 'RENEWALS/INCUMBENT';
       o.Abbv_Discount_Program__c = 'RENEW/INC';
     }
     else if ( o.Renewal_Opportunity__c == 'Yes'  && o.Renewal_Incumbant_Reseller__c == 'No' && o.Renewal_K_12__c == 'K-12' && o.Partner_Led__c == 'No' )
     {
      o.discount_program__c = 'RENEWALS/K-12';
      o.Abbv_Discount_Program__c = 'RENEW/K12';
     }
     else if ( o.Renewal_Opportunity__c == 'Yes'  && o.Renewal_Incumbant_Reseller__c == 'Yes' && o.Renewal_K_12__c == 'K-12' && o.Partner_Led__c == 'No' )
     {
      o.discount_program__c = 'RENEWALS/INCUMBENT/K-12';
      o.Abbv_Discount_Program__c = 'RENEW/INC/K12';
     }
    else if ( o.Renewal_Opportunity__c == 'Yes'  && o.Renewal_Incumbant_Reseller__c == 'No' && o.Renewal_K_12__c == 'Higher-Ed' && o.Partner_Led__c == 'No' )
    {
     o.discount_program__c = 'RENEWALS/HIGHER-ED';
     o.Abbv_Discount_Program__c = 'RENEW/HED';
    }
    else if ( o.Renewal_Opportunity__c == 'Yes'  && o.Renewal_Incumbant_Reseller__c == 'Yes' && o.Renewal_K_12__c == 'Higher-Ed' && o.Partner_Led__c == 'No' )
    {
     o.discount_program__c = 'RENEWALS/INCUMBENT/HIGHER-ED';
     o.Abbv_Discount_Program__c = 'RENEW/INC/HED';
    }  
    
    else if ( o.Renewal_Opportunity__c == 'Yes' && o.Renewal_Incumbant_Reseller__c == 'No' && o.Renewal_K_12__c == 'None' && o.Partner_Led__c == 'Yes'  ) 
     {
       o.discount_program__c = 'RENEWALS';
       o.Abbv_Discount_Program__c = 'RENEW';
     } 
     else if ( o.Renewal_Opportunity__c == 'Yes' && o.Renewal_Incumbant_Reseller__c == 'No' && o.Renewal_K_12__c == 'No' && o.Partner_Led__c == 'Yes'  ) 
     {
       o.discount_program__c = 'RENEWALS';
       o.Abbv_Discount_Program__c = 'RENEW';
     } 
     else if ( o.Renewal_Opportunity__c == 'Yes'  && o.Renewal_Incumbant_Reseller__c == 'Yes' && o.Renewal_K_12__c == 'None' && o.Partner_Led__c == 'Yes'  ) 
     {
       o.discount_program__c = 'RENEWALS/INCUMBENT';
       o.Abbv_Discount_Program__c = 'RENEW/INC';
     }
     else if ( o.Renewal_Opportunity__c == 'Yes'  && o.Renewal_Incumbant_Reseller__c == 'Yes' && o.Renewal_K_12__c == 'No' && o.Partner_Led__c == 'Yes'  ) 
     {
       o.discount_program__c = 'RENEWALS/INCUMBENT';
       o.Abbv_Discount_Program__c = 'RENEW/INC';
     }
     else if ( o.Renewal_Opportunity__c == 'Yes'  && o.Renewal_Incumbant_Reseller__c == 'No' && o.Renewal_K_12__c == 'K-12' && o.Partner_Led__c == 'Yes'  )
     {
      o.discount_program__c = 'RENEWALS/K-12';
      o.Abbv_Discount_Program__c = 'RENEW/K12';
     }
     else if ( o.Renewal_Opportunity__c == 'Yes'  && o.Renewal_Incumbant_Reseller__c == 'Yes' && o.Renewal_K_12__c == 'K-12' && o.Partner_Led__c == 'Yes'  )
     {
      o.discount_program__c = 'RENEWALS/INCUMBENT/K-12';
      o.Abbv_Discount_Program__c = 'RENEW/INC/K12';
     }
    else if ( o.Renewal_Opportunity__c == 'Yes'  && o.Renewal_Incumbant_Reseller__c == 'No' && o.Renewal_K_12__c == 'Higher-Ed' && o.Partner_Led__c == 'Yes'  )
    {
     o.discount_program__c = 'RENEWALS/HIGHER-ED';
     o.Abbv_Discount_Program__c = 'RENEW/HED';
    }
    else if ( o.Renewal_Opportunity__c == 'Yes'  && o.Renewal_Incumbant_Reseller__c == 'Yes' && o.Renewal_K_12__c == 'Higher-Ed' && o.Partner_Led__c == 'Yes'  )
    {
     o.discount_program__c = 'RENEWALS/INCUMBENT/HIGHER-ED';
     o.Abbv_Discount_Program__c = 'RENEW/INC/HED';
    }       
    // Commented by Sudhir for Deal Reg International 
    else if ( o.Renewal_Opportunity__c == 'No'  && o.Renewal_Incumbant_Reseller__c == 'No' && o.Renewal_K_12__c == 'None' && o.Partner_Led__c == 'Yes' )
    {
     o.discount_program__c      = 'NSP/PL';
     o.Abbv_Discount_Program__c = 'NSP/PL';
    }       
   else
   {
     o.discount_program__c      = 'NSP';
     o.Abbv_Discount_Program__c = 'NSP';       
   }    
  }   
 else if ( o.FORCE_NSP__c == NULL && o.Partner_Led__c == 'Yes' && o.discount_program__c <> 'NSP' )
    {
     o.discount_program__c      = 'NSP/PL';
     o.Abbv_Discount_Program__c = 'NSP/PL';       
     }    
 else if ( o.FORCE_NSP__c == NULL && o.Partner_Led__c == 'No' && o.discount_program__c <> 'NSP' )
    {
     o.discount_program__c      = 'NSP';
     o.Abbv_Discount_Program__c = 'NSP';       
     }   
 else
  {
   o.discount_program__c       = 'NSP';
   o.Abbv_Discount_Program__c  = 'NSP';    
   }
  
   //To Add Partner Level to Abberivation Discount Program  
   if ( o.Partner_Level__c == 'Authorized' ) 
    {
     o.Abbv_Discount_Program__c = 'AUTH/' + o.Abbv_Discount_Program__c;
    }
   else if ( o.Partner_Level__c == 'Gold' ) 
    {
     o.Abbv_Discount_Program__c = 'GOLD/' + o.Abbv_Discount_Program__c;
    }
   else if ( o.Partner_Level__c == 'Platinum' ) 
    {
     o.Abbv_Discount_Program__c = 'PLAT/' + o.Abbv_Discount_Program__c;
    }       
}
}
sudhIr NarayansudhIr Narayan
Hi All,

  After writing below test class i got 63% Please advice how to get 100%
 
@istest
public class NewDiscountProgramUpdateTest{

    public static testmethod void testOpportunityUpdate ()
    {
      
        
        Opportunity opp = new Opportunity();
        opp.Name = 'test Opportunity';
        opp.AccountId = '0013000000DULXG';     
        Opp.StageName='Closed Won';
        Opp.CloseDate = System.Today();
        opp.recordtypeId = '0123000000094yH';
        opp.ForecastCategoryName = 'Pipeline';
        opp.Government_Contract__c = 'None';
        opp.Type='Existing Customer';
        opp.Pricebook2Id='01s60000000AKxZAAW';
        opp.Primary_Competitor__c = 'No Competitor'; 
        opp.LeadSource='Renewal';
        opp.ownerid='00560000001vfE5AAI';
        opp.Channel_Source__c='Distributor';
        opp.Primary_Distributor__c = '0016000000Nt7j8';
        opp.Primary_Reseller__c = '0016000000Nt7j8';
        opp.Partner_Account__c = '0016000000Nt7j8';
        
        insert opp;
        
        opp.Partner_Driven__c = 'Yes';
        opp.Partner_Led__c = 'No';
        opp.K_12__c = 'No';
        update opp;

        opp.Partner_Driven__c = 'No';
        opp.Partner_Led__c = 'Yes';
        opp.K_12__c = 'No';
        update opp;
        
        opp.Partner_Driven__c = 'Yes';
        opp.Partner_Led__c = 'Yes';
        opp.K_12__c = 'No';
        update opp;
        
        opp.Partner_Driven__c = 'No';
        opp.Partner_Led__c = 'No';
        opp.K_12__c = 'Yes';
        update opp;
        
        opp.Partner_Driven__c = 'Yes';
        opp.Partner_Led__c = 'No';
        opp.K_12__c = 'Yes';
        update opp;
        
        opp.Partner_Driven__c = 'No';
        opp.Partner_Led__c = 'Yes';
        opp.K_12__c = 'Yes';
        update opp;
        
        opp.Partner_Driven__c = 'Yes';
        opp.Partner_Led__c = 'Yes';
        opp.K_12__c = 'Yes';
        update opp;
        
        opp.Renewal_Opportunity__c = NULL; 
        opp.Renewal_Incumbant_Reseller__c = NULL;
        opp.Renewal_K_12__c = NULL;
        opp.Partner_Led__c = 'No';
        update opp;
        
        opp.Renewal_Opportunity__c = 'Yes'; 
        opp.Renewal_Incumbant_Reseller__c = 'No';
        opp.Renewal_K_12__c = 'None';
        opp.Partner_Led__c = 'No';
        update opp;
        
        opp.Renewal_Opportunity__c = 'Yes'; 
        opp.Renewal_Incumbant_Reseller__c = 'No';
        opp.Renewal_K_12__c = 'No';
        opp.Partner_Led__c = 'No';
        update opp;       
 
        opp.Renewal_Opportunity__c = 'Yes'; 
        opp.Renewal_Incumbant_Reseller__c = 'Yes';
        opp.Renewal_K_12__c = 'None';
        opp.Partner_Led__c = 'No';
        update opp;
        
        opp.Renewal_Opportunity__c = 'Yes'; 
        opp.Renewal_Incumbant_Reseller__c = 'Yes';
        opp.Renewal_K_12__c = 'No';
        opp.Partner_Led__c = 'No';
        update opp;                
 
        opp.Renewal_Opportunity__c = 'Yes'; 
        opp.Renewal_Incumbant_Reseller__c = 'No';
        opp.Renewal_K_12__c = 'K-12';
        opp.Partner_Led__c = 'No';
        update opp; 
 
        opp.Renewal_Opportunity__c = 'Yes'; 
        opp.Renewal_Incumbant_Reseller__c = 'Yes';
        opp.Renewal_K_12__c = 'K-12';
        opp.Partner_Led__c = 'No';
        update opp; 
        
        opp.Renewal_Opportunity__c = 'Yes'; 
        opp.Renewal_Incumbant_Reseller__c = 'No';
        opp.Renewal_K_12__c = 'Higher-Ed';
        opp.Partner_Led__c = 'No';
        update opp;   
 
        opp.Renewal_Opportunity__c = 'Yes'; 
        opp.Renewal_Incumbant_Reseller__c = 'Yes';
        opp.Renewal_K_12__c = 'Higher-Ed';
        opp.Partner_Led__c = 'No';
        update opp;
        
        opp.Renewal_Opportunity__c = 'Yes'; 
        opp.Renewal_Incumbant_Reseller__c = 'No';
        opp.Renewal_K_12__c = 'None';
        opp.Partner_Led__c = 'Yes';
        update opp;
        
        opp.Renewal_Opportunity__c = 'Yes'; 
        opp.Renewal_Incumbant_Reseller__c = 'No';
        opp.Renewal_K_12__c = 'No';
        opp.Partner_Led__c = 'Yes';
        update opp;       
 
        opp.Renewal_Opportunity__c = 'Yes'; 
        opp.Renewal_Incumbant_Reseller__c = 'Yes';
        opp.Renewal_K_12__c = 'None';
        opp.Partner_Led__c = 'Yes';
        update opp;
        
        opp.Renewal_Opportunity__c = 'Yes'; 
        opp.Renewal_Incumbant_Reseller__c = 'Yes';
        opp.Renewal_K_12__c = 'No';
        opp.Partner_Led__c = 'Yes';
        update opp;                
 
        opp.Renewal_Opportunity__c = 'Yes'; 
        opp.Renewal_Incumbant_Reseller__c = 'No';
        opp.Renewal_K_12__c = 'K-12';
        opp.Partner_Led__c = 'Yes';
        update opp; 
 
        opp.Renewal_Opportunity__c = 'Yes'; 
        opp.Renewal_Incumbant_Reseller__c = 'Yes';
        opp.Renewal_K_12__c = 'K-12';
        opp.Partner_Led__c = 'Yes';
        update opp; 
        
        opp.Renewal_Opportunity__c = 'Yes'; 
        opp.Renewal_Incumbant_Reseller__c = 'No';
        opp.Renewal_K_12__c = 'Higher-Ed';
        opp.Partner_Led__c = 'Yes';
        update opp;   
 
        opp.Renewal_Opportunity__c = 'Yes'; 
        opp.Renewal_Incumbant_Reseller__c = 'Yes';
        opp.Renewal_K_12__c = 'Higher-Ed';
        opp.Partner_Led__c = 'Yes';
        update opp;
        
        opp.Renewal_Opportunity__c = 'No'; 
        opp.Renewal_Incumbant_Reseller__c = 'No';
        opp.Renewal_K_12__c = 'None';
        opp.Partner_Led__c = 'Yes';
        update opp;
                
        
        
    }
}