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
LaurenP6777LaurenP6777 

Test Class help for Trigger to Prevent Deletion

I am having alot of trouble writing a test class for this trigger. It is a simply trigger that stops a user from delete an oppty product if the Opportunity is a certain record type and in the stage "Closed Won". Can someone help? Thanks!

 

 

trigger NOEDDELETES on OpportunityLineItem (before Delete) {

for (OpportunityLineItem oli: trigger.old)

if(oli.Oppty_Stage__c=='Closed Won - Order'&&(oli.Related_Oppty_Record_Type__c=='BIOA'||oli.Related_Oppty_Record_Type__c=='BIOTECH'||oli.Related_Oppty_Record_Type__c=='DM'||oli.Related_Oppty_Record_Type__c=='CMC'||oli.Related_Oppty_Record_Type__c=='TOX'))

{

OLI.adderror('For Opportunities in stage Closed Won - Order, deleting products in not allowed. You must use the Dropped Product Process. Post Final Quote, you must use the Modification Process');

}
}

Kamatchi Devi SargunanathanKamatchi Devi Sargunanathan

 

 

Hi Lauren,

 

You need to write the test class for your trigger as follows,

 

@isTest(SeeAllData == true)
public class testNOEDDELETES{
  public static testMethod void test1(){
     public static testMethod void test1(){
     try{
         Pricebook2 pb = [select Id from PriceBook2 where IsStandard=True limit 1];
         Product2 product = new Product2(Name='Test Product',IsActive=true);
         Insert product ;  
               
        //Create a pricebook entry for custom pricebook
         PricebookEntry pbe1 = new PricebookEntry(UseStandardPrice = false, Pricebook2Id = pb.id,
                                                Product2Id = product.id,IsActive=true,UnitPrice=99);
         insert pbe1;
                 
         Opportunity objOpp1= new Opportunity(Name = 'Testing12',StageName = 'Upside',CloseDate = System.Today());
         insert objOpp1;
        
         OpportunityLineItem NewRec = new OpportunityLineItem(TotalPrice=99,Quantity=1,
                                                             Oppty_Stage__c='Closed Won - Order',
                                                             Related_Oppty_Record_Type__c='BIOA',
                                                             OpportunityId=objOpp1.Id,
                                                             PricebookEntryId=pbe1.id);
         insert NewRec;
          
         delete NewRec;
        
       }
       catch(Exception e){ }     
    }        
}

 

I'm just using the default values for the standard fiedls to insert dummy values. You include all your mandatory fields for inserting dummy values.

 

Try the above test class and inform me how much code coverage percentage you are getting for this trigger.

 

Hope this will help you...!

 

Please don't forget to give kudos by clicking on the Star icon and mark this as a solution, if this works out.