BACKGROUND
I have a simple trigger which is meant to block a record from being deleted based on the record type.
I am using a Trigger Handler by Kevin O'Hara
Trigger
trigger InvoiceTrigger on Invoice__c (before delete) {
new InvoiceTriggerHandler().run();
}
Trigger Handler
public class InvoiceTriggerHandler extends TriggerHandler {
public InvoiceTriggerHandler() {}
public override void beforeDelete(){
for(Invoice__c invoice : (List<Invoice__c>) Trigger.new) {
if (IsLocked(invoice)){
invoice.addError('You cannot delete an Invoice record which is locked. Please contact your Salesforce Administrator.');
}
}
}
private boolean IsLocked(Invoice__c invoice){
return invoice.RecordTypeId == InvoiceUtils.getLockedRecordTypeId;
}
}
Trigger Unit Test
@isTest
public class InvoiceTriggerTests {
@isTest
public static void InvoiceTrigger_Success(){
// arrange
Id invoiceId = TestDataFactory.createInvoice();
// act
Database.delete(invoiceId);
// assert
System.assert(true); // don't judge me :)
}
}
But when I run the unit test I get this error message:
Error Message
System.DmlException: Delete failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, InvoiceTrigger: execution of BeforeDelete
caused by: System.NullPointerException: Attempt to de-reference a null object
Class.InvoiceTriggerHandler.beforeDelete: line 7, column 1 Class.TriggerHandler.run: line 43, column 1 Trigger.InvoiceTrigger: line 2, column 1: []
QUESTIONS
- What is actually causing the problem?
- How can I fix it?