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

Test Class Not Covering Trigger.IsDelete Section


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.

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 =;
        Opp.AccountId =;
        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';

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

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

        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 =;
         testdeliv1.product2id =;
         testdeliv1.IsActive = True;
         testdeliv1.UnitPrice = 10000;
         testdeliv1.UseStandardPrice = false;
         PricebookEntry testdeliv2 = new PricebookEntry ();
         testdeliv2.pricebook2id =;
         testdeliv2.product2id =;
         testdeliv2.IsActive = True;
         testdeliv2.UnitPrice = 10000;
         testdeliv2.UseStandardPrice = false;
         PricebookEntry testdeliv3 = new PricebookEntry ();
         testdeliv3.pricebook2id =;
         testdeliv3.product2id =;
         testdeliv3.IsActive = True;
         testdeliv3.UnitPrice = 10000;
         testdeliv3.UseStandardPrice = false;

         Insert InsertPricebookList;

        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 =, OpportunityId = );

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

            oli1.add( new OpportunityLineItem(Quantity = 1, TotalPrice = 10000, 
            PriceBookEntryId =, OpportunityId = );
        insert oli1;
        delete oli1;

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.
Thanks.  Any thoughts on what I can do to try to figure out why it's not covering?