+ Start a Discussion
Chandu007Chandu007 

how to cover code coverage for Trigger.Isdelete

Hi,

I am able to cover Trigger.IsInsert & Trigger.IsUpdate but Trigger.IsDelete is not covering. Please check below test class and provide your suggestions.

Trigger:-
trigger CMC_Post_Tention_Weight_Rollup on OpportunityLineItem (after Insert, After Delete, After Update) 
{
Product2 productPostTensionWeight = [select id, name From Product2 where name = 'Post tension'];
    
if(trigger.isInsert)
{
List <OpportunityLineItem> postTensionWeight = trigger.new; //for update/Insert    
List<Id> listOptyIds = new List<Id>(); 
for(OpportunityLineItem eachOptyLineItem : PostTensionWeight ) 
    {
    listOptyIds.add(eachOptyLineItem.OpportunityId); 
    }

List <Opportunity> AllOppty = [Select ID, Name, Total_Post_Tension_Weight__c  from Opportunity 
                               where id in: listOptyIds];
for(Opportunity opty:AllOppty)
    {
    for(OpportunityLineItem PTW : postTensionWeight)
        {
          
        if(PTW.Product2Id == productPostTensionWeight.Id && PTW.OpportunityId == opty.Id)
            {   
            Opty.Total_Post_Tension_Weight__c = Opty.Total_Post_Tension_Weight__c + PTW.Post_Tension_Weight__c;   
            
            }
         }    
     }
update AllOppty;
}

if(trigger.isDelete)
{
List <OpportunityLineItem> postTensionWeight = trigger.Old; //for Delete    
List<Id> listOptyIds = new List<Id>(); 
for(OpportunityLineItem eachOptyLineItem : PostTensionWeight ) 
    {
    listOptyIds.add(eachOptyLineItem.OpportunityId); 
    }

List <Opportunity> AllOppty = [Select ID, Name, Total_Post_Tension_Weight__c  from Opportunity 
                               where id in: listOptyIds];
for(Opportunity opty:AllOppty)
    {
    for(OpportunityLineItem PTW : postTensionWeight)
        {
      
        if(PTW.Product2Id == productPostTensionWeight.Id && PTW.OpportunityId == opty.Id)
            {
           Opty.Total_Post_Tension_Weight__c = Opty.Total_Post_Tension_Weight__c - PTW.Post_Tension_Weight__c;   
            
            }
         }    
     }

update AllOppty;
}

if(trigger.isUpdate)
{
List <OpportunityLineItem> postTensionWeight = trigger.New;
List <OpportunityLineItem> OldpostTensionWeight = trigger.Old;    
List<Id> listOptyIds = new List<Id>(); 
for(OpportunityLineItem eachOptyLineItem : PostTensionWeight ) 
    {
    listOptyIds.add(eachOptyLineItem.OpportunityId); 
    }

List <Opportunity> AllOppty = [Select ID, Name, Total_Post_Tension_Weight__c  from Opportunity 
                               where id in: listOptyIds];
for(Opportunity opty:AllOppty)
    {
    for(OpportunityLineItem PTW : postTensionWeight)
        {
        For(OpportunityLineItem OPTW : OldpostTensionWeight)
        {        
        if(PTW.Product2Id == productPostTensionWeight.Id && PTW.OpportunityId == opty.Id && PTW.ID==OPTW.ID)
            {
      
            Opty.Total_Post_Tension_Weight__c = Opty.Total_Post_Tension_Weight__c - OPTW.Post_Tension_Weight__c + PTW.Post_Tension_Weight__c;   
            
            }
         }
        }     
     }

update AllOppty;
}
   


Test Class:- 

@istest(SeeAllData = true)
public with sharing class testupdateoppty{
 public static testMethod void TestdeletetonneageMethod() 
    {
      Opportunity op3 = new Opportunity(Name='Testoppthhhhhhssssde');
         
      op3.Bid_Date_Time__c = system.today();
      op3.CloseDate = system.today();
      op3.Market_Segment__c = 'Test1';
      op3.Stage_Structural__c = 'Planning';
      op3.State__c = 'AK';
      op3.City__c = 'Test';
      op3.Opportunity_Type__c = 'Test2';             
      op3.Referred_To__c = 'Test3';
      op3.Referred_From__c = 'Test4';
      op3.Sales_Office__c = 'Test6';
      op3.StageName = 'Planning';
      op3.Total_Loose_Dowel_Revenue__c = 1234;
      op3.Total_TBA_Revenue__c = 200000;
      op3.Total_Loose_Dowel_Revenue__c=1000;
      op3.Total_Loose_Dowel_Tons__c= 2000;
      op3.Dummy_Tonnage__c = 23 ;
      insert op3;
      
       Pricebook2 pb1 = [select Id from Pricebook2 where isStandard=true limit 1];                          
       Product2 prod1 = new Product2(Name = 'Loose Dowels123', ProductCode = 'LB');        
       insert prod1;
       
     
       
        PricebookEntry price = new PricebookEntry(
            
            Pricebook2Id = pb1.Id,
            UnitPrice = 1.0,
            IsActive = true,
            Product2Id = prod1.Id
            
            );
            insert price;
       
      
                 
      List<opportunityLineItem> opppro1 = new opportunitylineitem[]{new opportunitylineitem(UnitPrice=10000.00,
      Quantity=10,Total_Revenue__c = 10000.00, opportunityid=op3.id, pricebookEntryId=Price.id, Invoiced__c = TRUE)};
           
            insert opppro1;   
    
    op3.Dummy_Tonnage__c  = 50;
           update op3;
            
          Test.startTest();
           delete op3;
          Test.stopTest();
    }
}
 
Best Answer chosen by Chandu007
Sunil RathoreSunil Rathore
Hi chandu007,

Your trigger is on OpportunityLineItem object and you are deleting Opportunity object record (i.e. op3).
Please try with deletion of OpportunityLineItem object record in the test class.

Let me know if it solves your issue.

Many Thanks,
Sunil Rathore
 

All Answers

Ravi Dutt SharmaRavi Dutt Sharma
In the test class, add this line: delete opppro1;
Sunil RathoreSunil Rathore
Hi chandu007,

Your trigger is on OpportunityLineItem object and you are deleting Opportunity object record (i.e. op3).
Please try with deletion of OpportunityLineItem object record in the test class.

Let me know if it solves your issue.

Many Thanks,
Sunil Rathore
 
This was selected as the best answer
Chandu007Chandu007
Thanks Ravi & Sunil for pointing my silly mistake. :)
Deepali KulshresthaDeepali Kulshrestha
Hi chandu,

You have written the trigger on OpportunityLineItem so you have to insert , update and delete OpportunityLineItem record
but you have updated and deleted opportunity record . That's why your trigger is not firing at the time of deletetion of opportunity record.
Please try the following test class.

@istest(SeeAllData = true)
public with sharing class testupdateoppty{
 public static testMethod void TestdeletetonneageMethod() 
    {
      Opportunity op3 = new Opportunity(Name='Testoppthhhhhhssssde');
         
      op3.Bid_Date_Time__c = system.today();
      op3.CloseDate = system.today();
      op3.Market_Segment__c = 'Test1';
      op3.Stage_Structural__c = 'Planning';
      op3.State__c = 'AK';
      op3.City__c = 'Test';
      op3.Opportunity_Type__c = 'Test2';             
      op3.Referred_To__c = 'Test3';
      op3.Referred_From__c = 'Test4';
      op3.Sales_Office__c = 'Test6';
      op3.StageName = 'Planning';
      op3.Total_Loose_Dowel_Revenue__c = 1234;
      op3.Total_TBA_Revenue__c = 200000;
      op3.Total_Loose_Dowel_Revenue__c=1000;
      op3.Total_Loose_Dowel_Tons__c= 2000;
      op3.Dummy_Tonnage__c = 23 ;
      insert op3;
      
       Pricebook2 pb1 = [select Id from Pricebook2 where isStandard=true limit 1];                          
       Product2 prod1 = new Product2(Name = 'Loose Dowels123', ProductCode = 'LB');        
       insert prod1;
       
     
       
        PricebookEntry price = new PricebookEntry(
            
            Pricebook2Id = pb1.Id,
            UnitPrice = 1.0,
            IsActive = true,
            Product2Id = prod1.Id
            
            );
            insert price;
       
      
                 
      List<opportunityLineItem> opppro1 = new opportunitylineitem[]{new opportunitylineitem(UnitPrice=10000.00,
      Quantity=10,Total_Revenue__c = 10000.00, opportunityid=op3.id, pricebookEntryId=Price.id, Invoiced__c = TRUE)};
           Test.startTest();
           
            insert opppro1;   
    
           opppro1.Total_Revenue__c  = 50000;
           update op3;
            
          
           delete opppro1;
          Test.stopTest();
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha