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
travis.truetttravis.truett 

Test Fails After Adding Recursive Check

My apex text class for a trigger I wrote has started failing ever since I added code to the trigger to avoid recursion. As I understand it, I need to update my test in order to ensure the class I wrote to stop the recursion in the trigger is covered. Here's the class I'm using. My trigger calls it before it runs to ensure the trigger only fires once:

public Class checkRecursive{
    private static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}


And here's the test class:

@isTest (seeAllData=true)
private class testEditSchedule{

static testMethod void testUpdateSchedule() {
 
List<Account> aList = new List<Account>();    
Account a = new Account(
      Name = 'Test Account'
    );
    aList.add(a);
    
    insert aList;
    

List<Opportunity> oList = new List<Opportunity>();
Opportunity o = new Opportunity(
      Name = 'Test Opportunity', 
      StageName = 'Discovery', 
      CloseDate = Date.today(),
      Contract_Start_Date__c = Date.today(), 
      AccountId = a.Id
    );
    
    oList.add(o);
    insert oList;
    
        //Create Product
        Product2 prod = new Product2();
        prod.IsActive = true;
        prod.Name = 'Onboarding';
        prod.CanUseRevenueSchedule=true;
        insert prod;
        //Create PriceBook
        Pricebook2 pb = new Pricebook2();
        pb.Name = 'Test';
        pb.IsActive = true;
        insert pb;
        List<Pricebook2> PbList = [Select Id, Name, IsActive From Pricebook2 where IsStandard = true LIMIT 1];
    //Pricebook2 standardpb = [Select Id, Name, IsActive From Pricebook2 where IsStandard = true LIMIT 1];
        
            
        PricebookEntry standardpbe = new PricebookEntry();
        standardpbe.Pricebook2Id = PbList[0].Id;
        standardpbe.Product2Id = prod.Id;
        standardpbe.UnitPrice = 10000;
        standardpbe.IsActive = true;
        standardpbe.UseStandardPrice = false;
        insert standardpbe;
        
        PricebookEntry pbe = new PricebookEntry();
        pbe.Pricebook2Id = pb.Id;
        pbe.Product2Id = prod.Id;
        pbe.UnitPrice = 10000;
        pbe.IsActive = true;
        pbe.UseStandardPrice = false;
        insert pbe;
        
List<OpportunityLineItem> oliList = new List<OpportunityLineItem>(); 
List<OpportunityLineItem> oliAnnuallyList = new List<OpportunityLineItem>(); 
List<OpportunityLineItem> oliQuarterlyList = new List<OpportunityLineItem>(); 
List<OpportunityLineItem> oliMonthlyList = new List<OpportunityLineItem>(); 
       
OpportunityLineItem oliAnnually = new OpportunityLineItem(
        OpportunityId = o.Id,  
        PricebookEntryId = pbe.id,
        TotalPrice = 1,
        Quantity=2,
        Payment_Terms__c ='Annually',
        Duration__c = 12
        
    );
    OpportunityLineItem oliMonthly= new OpportunityLineItem(
        OpportunityId = o.Id,  
        PricebookEntryId = pbe.id,
        TotalPrice = 1,
        Quantity=2,
        Payment_Terms__c ='Monthly',
        Duration__c = 1
        
    );
    OpportunityLineItem oliQuarterly= new OpportunityLineItem(
        OpportunityId = o.Id,  
        PricebookEntryId = pbe.id,
        TotalPrice = 1,
        Quantity=2,
        Payment_Terms__c ='Quarterly',
        Duration__c = 3
        
    );
    OpportunityLineItem oli= new OpportunityLineItem(
        OpportunityId = o.Id,  
        PricebookEntryId = pbe.id,
        TotalPrice = 1,
        Quantity=2,
        Payment_Terms__c = 'Up Front',
        Duration__c = 12
        
    );
    oliAnnuallyList.add(oliAnnually);
    oliMonthlyList.add(oliMonthly);
    oliQuarterlyList.add(oliQuarterly);
    oliList.add(oli);
    insert oliList;
    insert oliAnnuallyList;
    insert oliQuarterlyList;
    insert oliMonthlyList;
    
    //Your trigger should have OpportunityLineItemSchedule logic then following asert will pass else it will fail
    List<OpportunityLineItem> lstOptyLi =[SELECT HasRevenueSchedule from OpportunityLineItem where Id =:oli.id];
    System.assertEquals(True, lstOptyLi[0].HasRevenueSchedule);
    List<OpportunityLineItem> lstOptyLi_2 =[SELECT HasRevenueSchedule from OpportunityLineItem where Id =:oliAnnually.id];
    System.assertEquals(True, lstOptyLi_2[0].HasRevenueSchedule);
    List<OpportunityLineItem> lstOptyLi_3 =[SELECT HasRevenueSchedule from OpportunityLineItem where Id =:oliQuarterly.id];
    System.assertEquals(True, lstOptyLi_3[0].HasRevenueSchedule);
    List<OpportunityLineItem> lstOptyLi_4 =[SELECT HasRevenueSchedule from OpportunityLineItem where Id =:oliMonthly.id];
    System.assertEquals(True, lstOptyLi_4[0].HasRevenueSchedule);
   
  }
    
}


What do I need to do to add coverage for the new class?

Thanks
Deepak GulianDeepak Gulian
//Set Static variable in test Class
className.run  = false;

 //Then do update or insert
Just do this in your test class!

 
travis.truetttravis.truett
Do I need to make my run variable public first?
Deepak GulianDeepak Gulian
Yes it should be this!
public static boolean run = false;

 
travis.truetttravis.truett
I tried adding that line, and the test still failed. Where specifically do I need to put it in the code?
Deepak GulianDeepak Gulian
Please share your trigger code!
travis.truetttravis.truett

trigger editSchedule on OpportunityLineItem (after insert) {

if(checkRecursive.runOnce())
    {

if(Trigger.isInsert){

Map<Id, Opportunity> opportunities = new Map<Id, Opportunity>();
Date myDate = Date.newInstance(1990, 2, 17);

for(OpportunityLineItem oli: Trigger.new) {
    opportunities.put(oli.OpportunityId, null);
}
opportunities.putAll([SELECT Contract_Start_Date__c FROM Opportunity WHERE Id in :opportunities.keySet()]);
for(OpportunityLineItem oli: Trigger.new) {
     Opportunity thisLineItemOpp = opportunities.get(oli.OpportunityId);
     myDate = thisLineItemOpp.Contract_Start_Date__c;
      // thisLineItemOpp.CloseDate is the close date you're looking for
}

List<OpportunityLineItemSchedule> listOLIS = new List<OpportunityLineItemSchedule>();




for (OpportunityLineItem oli: Trigger.new){
Set<Id> setOpptyOLI = new set<Id>();
setOpptyOLI.add(oli.Id);
List<OpportunityLineItem> OpptyOlilst=[Select Product2.Family FROM OpportunityLineItem WHERE Id IN:setOpptyOLI ];

Decimal DiscountedPrice;



DiscountedPrice = oli.Discounted_Price__c;


if(oli.Payment_Terms__c == 'Monthly'){

for(OpportunityLineItem record : OpptyOlilst){
if(record.Product2.Family=='Services'){
    
    
for(Integer duration = (Integer)oli.Duration__c; duration > 0; duration--){

listOLIS.add(new OpportunityLineItemSchedule(OpportunityLineItemId = oli.Id, Revenue = DiscountedPrice*oli.Quantity/oli.Duration__c, ScheduleDate = myDate, Type = 'Revenue'));
myDate = myDate.addMonths(1);

}//end of loop
    
    }//end of inner IF
    
else {
for(Integer duration = (Integer)oli.Duration__c; duration > 0; duration--){

listOLIS.add(new OpportunityLineItemSchedule(OpportunityLineItemId = oli.Id, Revenue = DiscountedPrice*oli.Quantity, ScheduleDate = myDate, Type = 'Revenue'));
myDate = myDate.addMonths(1);

}//end of loop

}//end of else

}//end of OpptyOlilstForLoop

}//end of Monthly IF


    else if(oli.Payment_Terms__c == 'Quarterly'){
    
for(OpportunityLineItem record : OpptyOlilst){
if(record.Product2.Family=='Services'){
    
for(Integer duration = (Integer)oli.Duration__c/3; duration > 0; duration--){

listOLIS.add(new OpportunityLineItemSchedule(OpportunityLineItemId = oli.Id, Revenue = DiscountedPrice*oli.Quantity*3/oli.Duration__c, ScheduleDate = myDate, Type = 'Revenue'));
myDate = myDate.addMonths(3);

}//end of loop
    
    }//end of inner IF

else{
for(Integer duration = (Integer)oli.Duration__c/3; duration > 0; duration--){

listOLIS.add(new OpportunityLineItemSchedule(OpportunityLineItemId = oli.Id, Revenue = DiscountedPrice*oli.Quantity*3, ScheduleDate = myDate, Type = 'Revenue'));
myDate = myDate.addMonths(3);

}//end of loop

}//end of else

}//end of OpptyOlilstForLoop

}//end of Quarterly IF


    else if(oli.Payment_Terms__c == 'Annually'){
    
for(OpportunityLineItem record : OpptyOlilst){
if(record.Product2.Family=='Services'){
          
    
for(Integer duration = (Integer)oli.Duration__c/12; duration > 0; duration--){

listOLIS.add(new OpportunityLineItemSchedule(OpportunityLineItemId = oli.Id, Revenue = DiscountedPrice*oli.Quantity*12/oli.Duration__c, ScheduleDate = myDate, Type = 'Revenue'));
myDate = myDate.addYears(1);

}//end of loop
    
    }//end of inner IF

else{
for(Integer duration = (Integer)oli.Duration__c/12; duration > 0; duration--){

listOLIS.add(new OpportunityLineItemSchedule(OpportunityLineItemId = oli.Id, Revenue = DiscountedPrice*oli.Quantity*12, ScheduleDate = myDate, Type = 'Revenue'));
myDate = myDate.addYears(1);

}//end of loop

}//end of else

}//end of OpptyOlilstForLoop

}//end of Annually IF


    else if(oli.Payment_Terms__c == 'Up Front'){
    
for(OpportunityLineItem record : OpptyOlilst){
if(record.Product2.Family=='Services'){
    
    
listOLIS.add(new OpportunityLineItemSchedule(OpportunityLineItemId = oli.Id, Revenue = DiscountedPrice*oli.Quantity, ScheduleDate = myDate, Type = 'Revenue'));
  
    }//end of inner IF

else{
listOLIS.add(new OpportunityLineItemSchedule(OpportunityLineItemId = oli.Id, Revenue = DiscountedPrice*oli.Quantity*oli.Duration__c, ScheduleDate = myDate, Type = 'Revenue'));

}//end of else

}//end of OpptyOlilstForLoop

}//end of Up Front IF 


insert listOLIS;



}//end of isInsert

}//end of runOnce

}//end of checkRecursive

}//end of trigger

I get errors in my assert statements