1

I've written a 'before delete' trigger to write to another object before a record is deleted. I've written a test class, but keep getting an error when I try to execute it. I'm new to Apex and have no idea why I'm getting this error. Below is my trigger and the test case that gives the error.

Before-delete Trigger

trigger GAL_PricingRule_Before_Delete on Pricing_Rule_vod__c (before delete) {

    if(trigger.isbefore && trigger.isdelete) {
        List <Price_Change__c> pclist = new List<Price_Change__c>();
        for(Pricing_Rule_vod__c pr : trigger.new){

//Get Record Type Id - List Price
            Id ListPr = Schema.SObjectType.Pricing_Rule_vod__c.getRecordTypeInfosByName().get('List_Price_Rule_vod').getRecordTypeId();

//Check for List Price
            If (pr.RecordtypeId == ListPr && pr.GAL_OLO_Pricing__c == TRUE) {

                Price_Change__c pc = new Price_Change__c (
                    GAL_Created_Date__c = System.now(),
                    GAL_description__c = pr.Name,
                    GAL_OLO_Integration__c = 'OLO Ready',
                    GAL_Record_Id__c = pr.Id,
                    GAL_Record_Type__c = 'List Price'
                );
                pclist.add(pc);
             }      
        }
        if(!pclist.isEmpty()){
            insert pclist;
        }
    }

}

Test Case The error relates to line 3:

static testMethod void TestDeleteListPriceOLO() {

@isTest
public class TestDeleteListPrice {
    static testMethod void TestDeleteListPriceOLO() {
//Set up test data
//Create dummy Pricing Rule
    Pricing_Rule_vod__c pr = new Pricing_Rule_vod__c (
        Name= 'Global_LP_Test_Deletion'
//        RecordTypeId = '01228000000QrNYAA0',
//        Product_vod__c = 'a002800000luFCQ',
//        Start_Date_vod__c = date.valueof('01/01/19'),
//        End_Date_vod__c = date.valueof('31/01/19')
    );        

    Test.startTest();
        insert pr;
    Test.stopTest();
// Trap any errors that might appear when a pricing rule is deleted
    Boolean errorThrown = false;
        try {

        delete pr;
        } 
        catch (Exception e) {
            System.debug(e);
            if (e.getMessage().contains('You can\'t delete this record')) {
                errorThrown = true;
            }
        }
    System.assert(errorThrown);
    }
}
  • I tried to save the code of your test class, did not get any error. Are you creating this test class as an inner class. Make sure there should not be any other outer class above it. – Nitish Singhal Jun 6 '17 at 2:32
  • Your test class looks fine to me. Test classes are saved just like normal classes, not executed through anonymous apex (which is one way to get the error you're seeing). Executing tests can be done through several different methods – Derek F Jun 6 '17 at 2:36
  • @Deb235 have you written above test class code in execute Anonymous section.? – Rohit Mourya Jun 6 '17 at 6:11
  • Thank you all for your replies. Yes, I'm getting the error in execute Anonymous section. I've since found that this error occurs in this section, (can't remember the reason), so I decided to go straight to testing. I've made some additional changes to pass the system validation, so now I just get one error on line 39. This is my test class now: – Deb235 Jun 7 '17 at 2:59
  • @isTest public class TestDeleteListPrice { static testMethod void TestDeleteListPriceOLO() { //Set up test data //Get Record Type Id - List Price Id ListPr = Schema.SObjectType.Pricing_Rule_vod__c.getRecordTypeInfosByName().get('List_Price_Rule_vod').getRecordTypeId(); //Create dummy Product Product_vod__c prod = new Product_vod__c ( Name = 'Test Product', Product_Type_vod__c = 'Order'); insert prod; – Deb235 Jun 7 '17 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.