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
GMASJGMASJ 

Recursive condition is not improving code coverage

Hi, 
  
   Added a recursive condition to a trigger to avoid too many soql error after than code coverage is 0% not at all improving please suggest me what might be the issue in the code. 

  Trigger : After recussive conidtion code coverage is 0%
trigger SyncRenwalQuote on Quote (After Insert, After Update, Before Delete) {

  Set<Id> setquoteid = new Set<id>();
  Set<string> setprodcode = new Set<string>();
  Set<id> setoppid = new Set<id>();
  String grecordtype;
  Boolean gsync = false;
  String gprioroppid;
  List<QuoteLineItem> listoli = new  List<QuoteLineItem> ();
  List< OpportunitylineItem> synlisoptln = new  List< OpportunitylineItem > ();
  List< OpportunitylineItem> nonsynlisoptln = new  List< OpportunitylineItem > ();
  Try 
  {
   If(SyncRenwalQuote_Recursive.runOnce()){
   
   if(trigger.isafter){
   
      for (Quote qt :  Trigger.new){
          setquoteid.add(qt.id);
       }
     
      list<Quote> Qt = [select IsSyncing ,opportunity.recordtype.name,opportunity.Prior_Year_Opportunity__c,
                                opportunityid
                           from Quote where id in :setquoteid and
                                opportunity.recordtype.name = 'Renewal Opportunity' and
                                opportunity.Prior_Year_Opportunity__c <> null
                              ];
 
         
        gsync = Qt[0].IsSyncing;
        gprioroppid = Qt[0].opportunity.Prior_Year_Opportunity__c;
      
       list<QuoteLineItem> qltln = [select id,quoteid,Prior_Discount__c,product2.productcode,
                                            quote.opportunityid,quote.opportunity.recordtype.name                   
                                     from QuoteLineItem where quoteid In :setquoteid]; 
                                     
                                      
         for(QuoteLineItem qtl : qltln){
            qtl.Prior_Discount__c = RenewalUtils.OppLineDisc(qtl.quote.opportunityid,qtl.quoteid,qtl.product2.productcode);
            qtl.Opportunity_Line_Item_ID__c = RenewalUtils.OppLineID(qtl.quote.opportunityid,qtl.quoteid,qtl.product2.productcode);
            listoli.add(qtl);
            setprodcode.add(qtl.product2.productcode);
            }
            
         if (!listoli.isEmpty()){
              update(listoli);
                                 
            }
                  
          
         if ( gsync == true){      
         
         // Uncheck opportunity product line in parent    
       list<OpportunityLineItem> opplstnonsyn = [select id, Renewed__c, opportunity.Prior_Year_Opportunity__c 
                                                    from OpportunityLineItem 
                                                    where opportunityid = :gprioroppid and 
                                                          product2.productcode <> :setprodcode];
                                                    
               for (OpportunitylineItem Oplnnonsync : opplstnonsyn){
                      Oplnnonsync.Renewed__c = false; 
                      nonsynlisoptln.add(Oplnnonsync);
                     }
                 
                    database.update(nonsynlisoptln);  
         
         // Check opportunity product line in parent                                                     
        list<OpportunityLineItem> opplstsyn = [select id, Renewed__c, opportunity.Prior_Year_Opportunity__c,
                                                    opportunity.syncedquoteid
                                            from OpportunityLineItem 
                                            where opportunityid = :gprioroppid and 
                                                  product2.productcode = :setprodcode];    
                                                  
               
               for (OpportunitylineItem Oplnsync : opplstsyn){
                      Oplnsync.Renewed__c = true; 
                      synlisoptln.add(Oplnsync);
                     }
                     
                  database.update(synlisoptln);   
      
          }    
        }
    }
 } 

catch(Exception e) {
    System.debug('The following exception has occurred: ' + e.getMessage());    
}
}
 Test Class
@isTest(seeAllData=true)
private class SyncRenwalQuote_test {

static testMethod void SyncRenwalQuote_test() {

 test.StartTest();
 
 If(SyncRenwalQuote_Recursive.runOnce()) return;

RecordType TY = [select id from RecordType  where DeveloperName = 'Standard_Opportunity' limit 1];
 
Account acc = new Account(Name = 'GMASJ + Sudhir + Sam');
insert acc;

//get standard pricebook
Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];

Pricebook2 pbk1 = new Pricebook2 (Name='Test Pricebook Entry 1',Description='Test Pricebook Entry 1', isActive=true);
insert pbk1;

Product2 prd1 = new Product2 (Name='Test Product Entry 1',Description='Test Product Entry 1',productCode = 'ABC', isActive = true);
insert prd1;


PricebookEntry pbe1 = new PricebookEntry (Product2ID=prd1.id,Pricebook2ID=standardPb.id,UnitPrice=50, isActive=true);
insert pbe1;


Opportunity opp1 = new Opportunity (Name='Opp1',StageName='Stage 0 - Lead Handed Off',CloseDate=Date.today(),Pricebook2Id = pbe1.Pricebook2Id, AccountId = acc.id,recordtypeid = TY.ID);
insert opp1;


OpportunityLineItem lineItem1 = new OpportunityLineItem (OpportunityID=opp1.id,PriceBookEntryID=pbe1.id, quantity=4, totalprice=200);
insert lineItem1;

RecordType RTY = [select id from RecordType  where DeveloperName = 'Renewal_Opportunity' limit 1];

Opportunity opp2 = new Opportunity (Name='Opp1',StageName='Stage 0 - Lead Handed Off',CloseDate=Date.today(),
                                    Pricebook2Id = pbe1.Pricebook2Id, AccountId = acc.id,recordtypeid = RTY.ID,
                                    Prior_Year_Opportunity__c=opp1.id);
insert opp2;


Quote q= new Quote ();
            q.Name= 'Testq';
            q.OpportunityId= Opp2.id;
            q.BillingStreet= '123';
            q.BillingCity= 'City';
            q.BillingPostalCode= '12345';
            q.Pricebook2Id= pbe1.Pricebook2Id;
            
            
            insert q;
         
            
 QuoteLineItem qli = new QuoteLineItem(
            QuoteId = q.Id, 
            PricebookEntryid= pbe1.Id,
            Quantity = 1, 
            UnitPrice = 500,
            Discount_Percent__c = 10,
            Subscription_Terms__c = 1,
            Number_of_Nodes__c = 10
            );
        insert qli;
        
             
  opp2.SyncedQuoteId = q.id;
  opp2.Prior_Year_Opportunity__c=opp1.id;
  String str = RenewalUtils.OppLineDisc(opp2.id,q.id,'CGOLD');
  SyncRenwalQuote_Recursive.run = true;  
    update opp2;    
  
   test.StopTest(); 
                   
 
}
}

 Recursive class 
public Class SyncRenwalQuote_Recursive{
    public static boolean run = true;
    public static boolean runOnce(){
    if(run){
     run=false;
     return true;
    }else{
        return run;
    }
    }
}

Please suggest me to fix this issue. 

Thanks
Sudhir
 
  
Best Answer chosen by GMASJ
Shweta_AgarwalShweta_Agarwal
Hi Sudhir,

I think you need to remove line no 8 If(SyncRenwalQuote_Recursive.runOnce()) return;
from test class.
Hope this will solve your issue.

Thanks
Shweta