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
GailGail 

Write test class for before delete trigger - deletion fails?

My trigger works fine when testing via the UI but when I try to delete an account via a test class, I get the error FATAL_ERROR|System.NullPointerException: Attempt to de-reference a null object.

 

trigger:

trigger AccountBeforeDelete on Account (before delete) {

Set<Id> TerritoryUpd = new Set<Id>();
Set<Id> StratDeleted = new Set<Id>();
List<Account> ChildUpd = new List<Account>();

For (Account a : trigger.old){
    
If (a.Territory_Test__c.startsWith('STRAT')) {  <--- this is the line that fails
    StratDeleted.add(a.id);
    }
}     
 ChildUpd = [SELECT Id FROM Account WHERE Ultimate_Parent_Acct__c in :StratDeleted];
 
 For (Account aa : ChildUpd) {
     TerritoryUpd.add(aa.id);
     }
    
   
  If(!TerritoryUpd.isEmpty()){
        Territory_Mgmt.Assign_Accounts(TerritoryUpd);
    }
}

 test class code snippet:

 test.starttest();
     insert up_strat;
     insert ch_strat;
     ch_strat.Ultimate_Parent_Acct__c = up_strat.Id;
     update ch_strat;
     Account up_strat_del = [Select id, Territory_Test__c FROM Account where ID = :up_strat.id];
     delete up_strat_del;
     test.stoptest();

 I used to try just deleting up_strat but saw something about querying for the inserted object and then deleting. That didn't work. When I put a try catch statement around the deletion, the deletion still failed with the same error. Why is my trigger not recognizing up_strat_del? 

Best Answer chosen by Admin (Salesforce Developers) 
Avidev9Avidev9

 

Probably Territory_Test__c is null for that record.

You just need to put a null check

 

trigger AccountBeforeDelete on Account (before delete) {

Set<Id> TerritoryUpd = new Set<Id>();
Set<Id> StratDeleted = new Set<Id>();
List<Account> ChildUpd = new List<Account>();

For (Account a : trigger.old){
    
If (a.Territory_Test__c!= NULL && a.Territory_Test__c.startsWith('STRAT')) {  <--- this is the line that fails
    StratDeleted.add(a.id);
    }
}     
 ChildUpd = [SELECT Id FROM Account WHERE Ultimate_Parent_Acct__c in :StratDeleted];
 
 For (Account aa : ChildUpd) {
     TerritoryUpd.add(aa.id);
     }
    
   
  If(!TerritoryUpd.isEmpty()){
        Territory_Mgmt.Assign_Accounts(TerritoryUpd);
    }
}

All Answers

Avidev9Avidev9

 

Probably Territory_Test__c is null for that record.

You just need to put a null check

 

trigger AccountBeforeDelete on Account (before delete) {

Set<Id> TerritoryUpd = new Set<Id>();
Set<Id> StratDeleted = new Set<Id>();
List<Account> ChildUpd = new List<Account>();

For (Account a : trigger.old){
    
If (a.Territory_Test__c!= NULL && a.Territory_Test__c.startsWith('STRAT')) {  <--- this is the line that fails
    StratDeleted.add(a.id);
    }
}     
 ChildUpd = [SELECT Id FROM Account WHERE Ultimate_Parent_Acct__c in :StratDeleted];
 
 For (Account aa : ChildUpd) {
     TerritoryUpd.add(aa.id);
     }
    
   
  If(!TerritoryUpd.isEmpty()){
        Territory_Mgmt.Assign_Accounts(TerritoryUpd);
    }
}
This was selected as the best answer
GailGail

bizarre, that fixed it even though Territory_Test__c WAS NOT null in the object getting deleted and my assertions to ensure the trigger code worked were fine. Thanks, muchly, Avi. 

GailGail

I lied, it was null. Now I've got to figure out how to ensure it is STRAT... that's my problem.