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 to Delete LineItems


I have a trigger that queries LineItems on an Opportunity and updates the Opportunity with counts.  The test class below returns coverage of 54% and is not covering deletion of line items.  I have a delete statement on line 78 that I thought would work, but is there something else I need to do?

Test Class:
// 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>();

        Product2 deliv1 = new Product2 (name='HR');
        deliv1.ProductCode = 'COACH_HR';
        deliv1.Product_Group__c = 'HR';

        Product2 deliv2 = new Product2 (name='MR');
        deliv2.productcode = 'COACH_MR';
        deliv2.Product_Group__c = 'MR';

        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;
         Insert InsertPricebookList;

        List<OpportunityLineItem> oli1 = new List<OpportunityLineItem>();
       // integer todo = 300;
       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 = );
      insert oli1;  
      delete oli1; 


Hi JN22,

I dont see any assert statements in your test class and its kind of hard for anyone to say why its not working. 

Since you said you are updating counts ,

Can you add assert to make sure your counts are matching, like when you insert line items, query Opportunity to get the count field and assert with inserted line item size.

Also, please add assert after insert statement by querying the line items to make sure they are all inserted fine 

This should help us know if you are getting expected result. Also will let us know why delete doesnt work.

Thank you

Thanks.  I've never used assert statements.  Would I just input someting like System.AssertNotEquals (null,oli1) at line 77?