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
Tarek Zaheer 1Tarek Zaheer 1 

Code Coverage - Test Class does not cover "After Update" with checkRecursive

Hi everyone, There have been some other posts on this exact same issue but none of those posted solutions have worked for me. I have a trigger that is firing twice when it should only be firing once. To solve this, I used checkRecursive (class pasted below). My trigger works fine now. However, my test class is not covering the section of my trigger which is isUpdate. This is due to the checkRecursive. 

Trigger:
 
trigger bsWarranty on QuoteLineItem (after insert,after update, after delete) {
     if(trigger.isinsert){
        for (QuoteLineItem QLI : Trigger.new) {
        for (Integer i = 0; i < QLI.Quantity; i++) {
            //if 0270-0200, add 0003-0129
        if ((QLI.PricebookEntryId == '01u61000003LhrX')) {
            
            QuoteLineItem newQLI = new QuoteLineItem();
            
            newQLI.Quantity        =   36;
            newQLI.QuoteId         =   QLI.QuoteId;
            newQLI.UnitPrice       =   0;
            newQLI.PricebookEntryId = '01u61000003LhqI';
                        
            insert newQLI;
        }}}
     for (QuoteLineItem QLI : Trigger.new) {
        for (Integer i = 0; i < QLI.Quantity; i++) {
           //if 0270-0404, add 0003-0374     
        if ((QLI.PricebookEntryId == '01u61000003LhpS')) {
            
            QuoteLineItem newQLI = new QuoteLineItem();
            
            newQLI.Quantity        =   36;
            newQLI.QuoteId         =   QLI.QuoteId;
            newQLI.UnitPrice       =   0;
            newQLI.PricebookEntryId = '01u61000003M03X';
                        
            insert newQLI;
        }}}
          for (QuoteLineItem QLI : Trigger.new) {
        for (Integer i = 0; i < QLI.Quantity; i++) {
            //if 0270-0870, add 0003-1179    
        if ((QLI.PricebookEntryId == '01u61000003LhpQ')) {
            
            QuoteLineItem newQLI = new QuoteLineItem();
            
            newQLI.Quantity        =   36;
            newQLI.QuoteId         =   QLI.QuoteId;
            newQLI.UnitPrice       =   0;
            newQLI.PricebookEntryId = '01u61000003LhqA';
                        
            insert newQLI;
        }}}
         for (QuoteLineItem QLI : Trigger.new) {
        for (Integer i = 0; i < QLI.Quantity; i++) {
            
        if ((QLI.PricebookEntryId == '01u61000003LhpR')|| //0270-0912 
           (QLI.PricebookEntryId == '01u61000003LhpX') || //0270-0878
           (QLI.PricebookEntryId == '01u61000003Lhpa') || //0270-0747
           (QLI.PricebookEntryId == '01u61000003Lhpb')) { //0270-0749
            //if above, add 0003-0878
            QuoteLineItem newQLI = new QuoteLineItem();
            system.debug(QLI.Quantity);
            newQLI.Quantity        =   36;
            newQLI.QuoteId         =   QLI.QuoteId;
            newQLI.UnitPrice       =   0;
            newQLI.PricebookEntryId = '01u61000003Lhpn';
                        
            insert newQLI;
        }}}
         for (QuoteLineItem QLI : Trigger.new) {
        for (Integer i = 0; i < QLI.Quantity; i++) {
            
        if ((QLI.PricebookEntryId == '01u61000003LhpT')|| //0270-0860
           (QLI.PricebookEntryId == '01u61000003LhpU') || //0270-0858
           (QLI.PricebookEntryId == '01u61000003LhpV') || //0270-0821
           (QLI.PricebookEntryId == '01u61000003LhpW') || //0270-0822
           (QLI.PricebookEntryId == '01u61000003LhpY') || //0270-0817
           (QLI.PricebookEntryId == '01u61000003LhpZ') || //0270-0818
           (QLI.PricebookEntryId == '01u61000003Lhpc') || //0270-0604
           (QLI.PricebookEntryId == '01u61000003Lhpe'))   //0270-0415
           //if above, add 0003-1085
        {
            
            QuoteLineItem newQLI = new QuoteLineItem();
            
            newQLI.Quantity        =   36;
            newQLI.QuoteId         =   QLI.QuoteId;
            newQLI.UnitPrice       =   0;
            newQLI.PricebookEntryId = '01u61000003Lhpy';
                        
            insert newQLI;
        }}}
          for (QuoteLineItem QLI : Trigger.new) {
        for (Integer i = 0; i < QLI.Quantity; i++) {
           //if 0270-0793 or 0270-0790 (not currently on contract), add 0003-0259     
        if ((QLI.PricebookEntryId == '01u61000003Lhpg')||
           (QLI.PricebookEntryId == '01u61000003Lhpd')) {
            
            QuoteLineItem newQLI = new QuoteLineItem();
            
            newQLI.Quantity        =   36;
            newQLI.QuoteId         =   QLI.QuoteId;
            newQLI.UnitPrice       =   0;
            newQLI.PricebookEntryId = '01u61000003LhqU';
                        
            insert newQLI;
        }}}}
        
        
        if (trigger.isupdate)   {
            
    if(RecursiveCheck.runOnce()){
               
        for (QuoteLineItem QLInew : Trigger.new){            
        Decimal QuantityNew = QLInew.Quantity;
            QuoteLineItem QLIold = Trigger.oldMap.get(QLInew.id);
        Decimal QuantityOld = QLIold.Quantity;    
        system.debug('@@@@@@@@@@'+QuantityOld);
            system.debug('@@@@@@@@@@'+QuantityNew);
            
        if (((QuantityNew != QuantityOld) &&
           (QLInew.Quote.Pricebook2.id == '01s61000003U9Id') &&
           ((QLInew.PricebookEntryId == '01u61000003LhrX') || // 0270-0200
            (QLInew.PricebookEntryId == '01u61000003LhpS') || // 0270-0404
            (QLInew.PricebookEntryId == '01u61000003LhpQ') || // 0270-0870
            (QLInew.PricebookEntryId == '01u61000003LhpR') || // 0270-0912
            (QLInew.PricebookEntryId == '01u61000003LhpX') || // 0270-0878
            (QLInew.PricebookEntryId == '01u61000003Lhpa') || // 0270-0747
            (QLInew.PricebookEntryId == '01u61000003Lhpb') || // 0270-0749
            (QLInew.PricebookEntryId == '01u61000003LhpT') || // 0270-0860
            (QLInew.PricebookEntryId == '01u61000003LhpU') || // 0270-0858
            (QLInew.PricebookEntryId == '01u61000003LhpV') || // 0270-0821
            (QLInew.PricebookEntryId == '01u61000003LhpW') || // 0270-0822
            (QLInew.PricebookEntryId == '01u61000003LhpY') || // 0270-0817
            (QLInew.PricebookEntryId == '01u61000003LhpZ') || // 0270-0818
            (QLInew.PricebookEntryId == '01u61000003Lhpc') || // 0270-0604
            (QLInew.PricebookEntryId == '01t61000001D7y6') || // 0270-0415
            (QLInew.PricebookEntryId == '01u61000003Lhpd') || // 0270-0793
            (QLInew.PricebookEntryId == '01u61000003Lhpg') //0270-0790
)))             
             {  

         system.debug(QuantityNew);
            system.debug(QuantityOld);
         
         List<QuotelineItem> zeroQLI = [SELECT id FROM QuoteLineItem WHERE UnitPrice = 0];
            delete zeroQLI;   
            }
                 
              for (QuoteLineItem QLI : Trigger.new) {
        for (Integer i = 0; i < QLI.Quantity; i++) {
            //if 0270-0200, add 0003-0129
        if ((QLI.PricebookEntryId == '01u61000003LhrX') && (QuantityNew != QuantityOld)) {
            
            QuoteLineItem newQLI = new QuoteLineItem();
            
            newQLI.Quantity        =   36;
            newQLI.QuoteId         =   QLI.QuoteId;
            newQLI.UnitPrice       =   0;
            newQLI.PricebookEntryId = '01u61000003LhqI';
                        
            insert newQLI;
        }}}
     for (QuoteLineItem QLI : Trigger.new) {
        for (Integer i = 0; i < QLI.Quantity; i++) {
           //if 0270-0404, add 0003-0374     
        if ((QLI.PricebookEntryId == '01u61000003LhpS') && (QuantityNew != QuantityOld)) {
            
            QuoteLineItem newQLI = new QuoteLineItem();
            
            newQLI.Quantity        =   36;
            newQLI.QuoteId         =   QLI.QuoteId;
            newQLI.UnitPrice       =   0;
            newQLI.PricebookEntryId = '01u61000003M03X';
                        
            insert newQLI;
        }}}
          for (QuoteLineItem QLI : Trigger.new) {
        for (Integer i = 0; i < QLI.Quantity; i++) {
            //if 0270-0870, add 0003-1179    
        if ((QLI.PricebookEntryId == '01u61000003LhpQ') && (QuantityNew != QuantityOld)) {
            
            QuoteLineItem newQLI = new QuoteLineItem();
            
            newQLI.Quantity        =   36;
            newQLI.QuoteId         =   QLI.QuoteId;
            newQLI.UnitPrice       =   0;
            newQLI.PricebookEntryId = '01u61000003LhqA';
                        
            insert newQLI;
        }}}
           for (QuoteLineItem QLI : Trigger.new) {
        for (Integer i = 0; i < QLI.Quantity; i++) {

        if ((QLI.PricebookEntryId == '01u61000003LhpR')|| //0270-0912
           (QLI.PricebookEntryId == '01u61000003LhpX') || //0270-0878
           (QLI.PricebookEntryId == '01u61000003Lhpa') || //0270-0747
           (QLI.PricebookEntryId == '01u61000003Lhpb')    //0270-0749
           && (QuantityNew != QuantityOld)) {
            //if above, add 0003-0878
            QuoteLineItem newQLI = new QuoteLineItem();
            system.debug(QuantityNew);
            system.debug(QuantityOld);
            newQLI.Quantity        =   36;
            newQLI.QuoteId         =   QLI.QuoteId;
            newQLI.UnitPrice       =   0;
            newQLI.PricebookEntryId = '01u61000003Lhpn';
                        
               insert newQLI;
        }}}
         for (QuoteLineItem QLI : Trigger.new) {
        for (Integer i = 0; i < QLI.Quantity; i++) {
            
        if ((QLI.PricebookEntryId == '01u61000003LhpT')|| //0270-0860
           (QLI.PricebookEntryId == '01u61000003LhpU') || //0270-0858
           (QLI.PricebookEntryId == '01u61000003LhpV') || //0270-0821
           (QLI.PricebookEntryId == '01u61000003LhpW') || //0270-0822
           (QLI.PricebookEntryId == '01u61000003LhpY') || //0270-0817
           (QLI.PricebookEntryId == '01u61000003LhpZ') || //0270-0818
           (QLI.PricebookEntryId == '01u61000003Lhpc') || //0270-0604
           (QLI.PricebookEntryId == '01t61000001D7y6')    //0270-0415
           && (QuantityNew != QuantityOld))
           //if above, add 0003-1085
        {
            
            QuoteLineItem newQLI = new QuoteLineItem();
            
            newQLI.Quantity        =   36;
            newQLI.QuoteId         =   QLI.QuoteId;
            newQLI.UnitPrice       =   0;
            newQLI.PricebookEntryId = '01u61000003Lhpy';
                        
            insert newQLI;
        }}}
          for (QuoteLineItem QLI : Trigger.new) {
        for (Integer i = 0; i < QLI.Quantity; i++) {
           //if 0270-0793 or 0270-0790 (not currently on contract), add 0003-0259     
        if ((QLI.PricebookEntryId == '01u61000003Lhpd')||
            (QLI.PricebookEntryId == '01u61000003Lhpg')
           && (QuantityNew != QuantityOld)) {
            
            QuoteLineItem newQLI = new QuoteLineItem();
            
            newQLI.Quantity        =   36;
            newQLI.QuoteId         =   QLI.QuoteId;
            newQLI.UnitPrice       =   0;
            newQLI.PricebookEntryId = '01u61000003LhqU';
                        
            insert newQLI;
           }}}}}}
        

        if(trigger.isdelete){
         for (QuoteLineItem QLInew : Trigger.old){            
           //  if (QLInew.Quote.Pricebook2.Name == 'Bon Secours (exp 1/5/2020)'){
        List<QuotelineItem> zeroQLI = [SELECT id FROM QuoteLineItem WHERE UnitPrice = 0];
        delete zeroQLI;
         }}}

Test Class:
 
@isTest(SeeAllData=true)
public class bsWarranty_Test {
    
    static testMethod void testBonSecoursWarranty(){
        
        Account acct = New Account();
        
        acct.Name                =   'Test Account';
        acct.PC_Territory__c     =   '1234'; 
        acct.CC_Territory__c     =   '1234';
        acct.Facility_Type__c    =   'Acute';
        acct.ShippingCountryCode =   'US';
        acct.ShippingStreet      =   '1234 Test Lane';
        acct.ShippingPostalCode  =   '12345';
        acct.ShippingStateCode   =   'WA';
         
        insert acct;
        
        Opportunity opp = New Opportunity();
        
        opp.Name = 'Test';
        opp.CloseDate = Date.today();
        opp.CurrencyIsoCode = 'USD';
        opp.AccountId = acct.id;
        opp.StageName = 'Qualified';
        opp.Business_Type__c = 'Patient care';
        opp.RecordTypeId = '012610000002W6nAAE';
        opp.Pricebook2Id = '01s61000003U9Id';
        
        insert opp;
        
        Quote quote = New Quote();
        quote.Name = 'Test';
        quote.ExpirationDate = date.today();
        quote.ShippingHandling = 0;
        quote.Ship_Date__c = date.today();
        quote.OpportunityId = opp.id;
        quote.Pricebook2Id = '01s61000003U9Id';
        insert quote;
        
        QuoteLineItem QLI = New QuoteLineItem();
        qli.QuoteId = Quote.Id;
        qli.PricebookEntryId = '01u61000003LhpS';
        qli.UnitPrice = 0;
        qli.Quantity = 1;
        insert QLI;
        
        QuoteLineItem QLI1 = New QuoteLineItem();
        qli1.QuoteId = Quote.Id;
        qli1.PricebookEntryId = '01u61000003LhrX';
        qli1.UnitPrice = 0;
        qli1.Quantity = 1;
        insert QLI1;
        
        QuoteLineItem QLI2 = New QuoteLineItem();
        qli2.QuoteId = Quote.Id;
        qli2.PricebookEntryId = '01u61000003LhpQ';
        qli2.UnitPrice = 0;
        qli2.Quantity = 1;
        insert QLI2;
        
        QuoteLineItem QLI3 = New QuoteLineItem();
        qli3.QuoteId = Quote.Id;
        qli3.PricebookEntryId = '01u61000003LhpR';
        qli3.UnitPrice = 0;
        qli3.Quantity = 1;
        insert QLI3;
        
        QuoteLineItem QLI4 = New QuoteLineItem();
        qli4.QuoteId = Quote.Id;
        qli4.PricebookEntryId = '01u61000003LhpT';
        qli4.UnitPrice = 0;
        qli4.Quantity = 1;
        insert QLI4; 
        
        QuoteLineItem QLI5 = New QuoteLineItem();
        qli5.QuoteId = Quote.Id;
        qli5.PricebookEntryId = '01u61000003Lhpg';
        qli5.UnitPrice = 0;
        qli5.Quantity = 1;
        insert QLI5;
        
        Quote quote1 = New Quote();
        quote1.Name = 'Test1';
        quote1.ExpirationDate = date.today();
        quote1.ShippingHandling = 0;
        quote1.Ship_Date__c = date.today();
        quote1.OpportunityId = opp.id;
        quote1.Pricebook2Id = '01s61000003U9Id';
        insert quote1;
        
        QuoteLineItem QLI6 = New QuoteLineItem();
        qli6.QuoteId = quote1.Id;
        qli6.PricebookEntryId = '01u61000003Lhpg';
        qli6.UnitPrice = 0;
        qli6.Quantity = 1;
        insert QLI6;
        
        QLI6.Quantity = 2;
        update QLI6;
        
        delete QLI6;
               
    }
}

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

If someone can point out how I need to change one of the three above blocks of code, that would be much appreciated. I'm getting good code coverage except under the IsUpdate clause due to the RecursiveCheck. 

Thank you!
FearNoneFearNone
tarek,

can you try to separate the test method for insert and update.
Separate transaction will null out the static methods/variables, so RecursiveCheck will be reset on each transaction.
But the catch is you need to have a data already in database before the update-test.
Tarek Zaheer 1Tarek Zaheer 1
Hi FearNone, thank you for the suggestion. Do you know how I would change the test class to separate it for insert/update? I thought explicitly declaring insert and update DML actions in the test class would do the trick. If I need to split it into two transactions, how would I do so?
FearNoneFearNone
hi tarek,

inserting then updating could have been a good and easy test plan, but you said update codes are not executed because of the runOnce condition (if I understood it correctly)...

splitting it means that you have two create two functions. one for the insert and one for the update. you can retain the above test-function as for the insert. then create a new test function for the update bacause you already have the @isTest(SeeAllData=true) annotation.
@isTest(SeeAllData=true)
public class bsWarranty_Test {
    
    static testMethod void testBonSecoursWarranty(){
        ...    
    }
    
    static testMethod void test2(){
        // update action only
    }
}

 
Tarek Zaheer 1Tarek Zaheer 1
Hi FearNone, thanks for your response and the clear instructions... unfortunately I tried just that and received an error upon trying to save:

"Only top-level class variables can be declared static"

Thoughts?
Tarek Zaheer 1Tarek Zaheer 1
My mistake, I missed a bracket. I made your suggested change, saved, ran the test and no change... it still is not covering the IsInsert clause. 

For reference, here is the latest code below. Any thoughts as to why it's still not working?
 
@isTest(SeeAllData=true)
public class bsWarranty_Test {
    
    static testMethod void testbsWarranty(){
       

        Account acct = New Account();
        
        acct.Name                =   'Test Account';
        acct.PC_Territory__c     =   '1234'; 
        acct.CC_Territory__c     =   '1234';
        acct.Facility_Type__c    =   'Acute';
        acct.ShippingCountryCode =   'US';
        acct.ShippingStreet      =   '1234 Test Lane';
        acct.ShippingPostalCode  =   '12345';
        acct.ShippingStateCode   =   'WA';
         
        insert acct;
        
        Opportunity opp = New Opportunity();
        
        opp.Name = 'Test';
        opp.CloseDate = Date.today();
        opp.CurrencyIsoCode = 'USD';
        opp.AccountId = acct.id;
        opp.StageName = 'Qualified';
        opp.Business_Type__c = 'Patient care';
        opp.RecordTypeId = '012610000002W6nAAE';
        opp.Pricebook2Id = '01s61000003U9Id';
        
        insert opp;
        
        Quote quote = New Quote();
        quote.Name = 'Test';
        quote.ExpirationDate = date.today();
        quote.ShippingHandling = 0;
        quote.Ship_Date__c = date.today();
        quote.OpportunityId = opp.id;
        quote.Pricebook2Id = '01s61000003U9Id';
        insert quote;
        
        QuoteLineItem QLI = New QuoteLineItem();
        qli.QuoteId = Quote.Id;
        qli.PricebookEntryId = '01u61000003LhpS';
        qli.UnitPrice = 0;
        qli.Quantity = 1;
        insert QLI;
        
        QuoteLineItem QLI1 = New QuoteLineItem();
        qli1.QuoteId = Quote.Id;
        qli1.PricebookEntryId = '01u61000003LhrX';
        qli1.UnitPrice = 0;
        qli1.Quantity = 1;
        insert QLI1;
        
        QuoteLineItem QLI2 = New QuoteLineItem();
        qli2.QuoteId = Quote.Id;
        qli2.PricebookEntryId = '01u61000003LhpQ';
        qli2.UnitPrice = 0;
        qli2.Quantity = 1;
        insert QLI2;
        
        QuoteLineItem QLI3 = New QuoteLineItem();
        qli3.QuoteId = Quote.Id;
        qli3.PricebookEntryId = '01u61000003LhpR';
        qli3.UnitPrice = 0;
        qli3.Quantity = 1;
        insert QLI3;
        
        QuoteLineItem QLI4 = New QuoteLineItem();
        qli4.QuoteId = Quote.Id;
        qli4.PricebookEntryId = '01u61000003LhpT';
        qli4.UnitPrice = 0;
        qli4.Quantity = 1;
        insert QLI4; 
        
        QuoteLineItem QLI5 = New QuoteLineItem();
        qli5.QuoteId = Quote.Id;
        qli5.PricebookEntryId = '01u61000003Lhpg';
        qli5.UnitPrice = 0;
        qli5.Quantity = 1;
        insert QLI5;
        
        Quote quote1 = New Quote();
        quote1.Name = 'Test1';
        quote1.ExpirationDate = date.today();
        quote1.ShippingHandling = 0;
        quote1.Ship_Date__c = date.today();
        quote1.OpportunityId = opp.id;
        quote1.Pricebook2Id = '01s61000003U9Id';
        insert quote1;
        
        QuoteLineItem QLI6 = New QuoteLineItem();
        qli6.QuoteId = quote1.Id;
        qli6.PricebookEntryId = '01u61000003Lhpg';
        qli6.UnitPrice = 0;
        qli6.Quantity = 1;
        insert QLI6;
        
        QLI6.Quantity = 2;
        update QLI6;
        
        delete QLI6;
    }
            
static testMethod void test2(){
        Account acct = New Account();
        
        acct.Name                =   'Test Account';
        acct.PC_Territory__c     =   '1234'; 
        acct.CC_Territory__c     =   '1234';
        acct.Facility_Type__c    =   'Acute';
        acct.ShippingCountryCode =   'US';
        acct.ShippingStreet      =   '1234 Test Lane';
        acct.ShippingPostalCode  =   '12345';
        acct.ShippingStateCode   =   'WA';
         
        insert acct;
        
        Opportunity opp = New Opportunity();
        
        opp.Name = 'Test';
        opp.CloseDate = Date.today();
        opp.CurrencyIsoCode = 'USD';
        opp.AccountId = acct.id;
        opp.StageName = 'Qualified';
        opp.Business_Type__c = 'Patient care';
        opp.RecordTypeId = '012610000002W6nAAE';
        opp.Pricebook2Id = '01s61000003U9Id';
        
        insert opp;
        
        Quote quote = New Quote();
        quote.Name = 'Test';
        quote.ExpirationDate = date.today();
        quote.ShippingHandling = 0;
        quote.Ship_Date__c = date.today();
        quote.OpportunityId = opp.id;
        quote.Pricebook2Id = '01s61000003U9Id';
        insert quote;
        
        QuoteLineItem QLI = New QuoteLineItem();
        qli.QuoteId = Quote.Id;
        qli.PricebookEntryId = '01u61000003LhpS';
        qli.UnitPrice = 0;
        qli.Quantity = 1;
        insert QLI;
    }
}

 
FearNoneFearNone
hi tarek,

i mimic your first code and tried it on my environment, but the trigger.update was executed.
can you post screenshot of your test result.
User-added image

or did I misunderstood your problem that trigger.isupdate was not executed.