+ Start a Discussion
JN22JN22 

Test Class Not Covering Trigger.IsDelete Section

Hello,

I have a trigger the queries the OpportunityLineItem records on an Opportunity, looks for certain product families, and then updates the Opportunity record with the roll-up amount from each family and checks a box if that family is represented in the OpportunityLineItems.  The trigger works fine.  The test below covers 54% of the trigger, but what it is not covering is everything in the trigger after the line "IF(trigger.isdelete){".  I didn't post the trigger because it is very large.  Does anyone know why the "delete oli1;" line (line 94) in the test class below would not cover the delete portion of the trigger and how to fix it?  Thanks!

// This class tests the trigger named OppUpdates.

@isTest(seeAllData=true)
private class TestOppUpdates {

    static testMethod void TestOppUpdates() {       
                    
//Data Prep - Create Account, Opportunity, Product, etc.

        Account acct = new Account(name='Test Account', Type = 'Employer');
        insert acct;
        
    //Create Opportunity on Account
        Opportunity Opp = new Opportunity(Name='Test Account - New Opp1');
        Opp.StageName = 'Stage 1 - Learn';
        Opp.CloseDate = Date.today();
        Opp.AccountId = acct.id;
        insert Opp;         
                       
    // Create Deliverables 

    List<product2> Insertprodlist= new list<product2>();

        //High-Risk Coaching
        Product2 deliv1 = new Product2 (name='Coaching-HR');
        deliv1.ProductCode = 'COACH_HR';
        deliv1.Product_Group__c = 'Coaching';
        Insertprodlist.add(deliv1);

        //Moderate-Risk Coaching
        Product2 deliv2 = new Product2 (name='Coaching-MR');
        deliv2.productcode = 'COACH_MR';
        deliv2.Product_Group__c = 'Coaching';
        Insertprodlist.add(deliv2);

        //Low-Risk Coaching
        Product2 deliv3 = new Product2 (name='Coaching-LR');
        deliv3.productcode = 'COACH_LR';
        deliv3.Product_Group__c = 'Coaching';
        Insertprodlist.add(deliv3);

        
        insert Insertprodlist;
        
    // Get Pricebook
         Pricebook2 testpb = [select id from Pricebook2 where IsStandard = true];   
         
         List<PricebookEntry> InsertPricebookList= new List<PricebookEntry>();

// Add to pricebook
         PricebookEntry testdeliv1 = new PricebookEntry ();
         testdeliv1.pricebook2id = testpb.id;
         testdeliv1.product2id = deliv1.id;
         testdeliv1.IsActive = True;
         testdeliv1.UnitPrice = 10000;
         testdeliv1.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv1);
         
         PricebookEntry testdeliv2 = new PricebookEntry ();
         testdeliv2.pricebook2id = testpb.id;
         testdeliv2.product2id = deliv2.id;
         testdeliv2.IsActive = True;
         testdeliv2.UnitPrice = 10000;
         testdeliv2.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv2);
         
         PricebookEntry testdeliv3 = new PricebookEntry ();
         testdeliv3.pricebook2id = testpb.id;
         testdeliv3.product2id = deliv3.id;
         testdeliv3.IsActive = True;
         testdeliv3.UnitPrice = 10000;
         testdeliv3.UseStandardPrice = false;
         InsertPricebookList.add(testdeliv3);

         Insert InsertPricebookList;
         

test.starttest();
        List<OpportunityLineItem> oli1 = new List<OpportunityLineItem>();
            integer todo = 20;
            for(integer bi=0; bi<todo; bi++) {
        
            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv1.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv2.id, OpportunityId = Opp.id) );

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId = testdeliv3.id, OpportunityId = Opp.id) );
         }
        insert oli1;
        
        delete oli1;
        
test.stoptest();
    }
}


bob_buzzardbob_buzzard
I assume you aren't getting any errors from your test class?  There's no obvious reason why a delete wouldn't cause the trigger to execute, unless the trigger.isDelete condition is unreachable in your trigger, but from what you say the trigger is working correctly.
JN22JN22
Thanks.  Any thoughts on what I can do to try to figure out why it's not covering?