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
WeylandYutanieWeylandYutanie 

Must be a silly mistake

Hi, I am getting the following error when trying to update a field on the before deletion of an object:

LeavingTheLine: execution of BeforeDelete
caused by: System.NullPointerException: Attempt to de-reference a null object
Trigger.LeavingTheLine: line 5, column 9

I must be missing something really obvious. Any help appreciated.

Weyland


Code:
1 trigger LeavingTheLine on Place_in_Line__c (before delete) 
2 {
3     for (Place_in_Line__c SpotInLine : Trigger.old)
4     {
5          SpotInLine.Group_Leader_Line__r.Number_of_People_in_Line__c = 1;
6     }
7 }



Best Answer chosen by Admin (Salesforce Developers) 
Shashikant SharmaShashikant Sharma

In your trigger , you are trying to access a related object's field directly from trigger.old list's item. Triger.old only can give you values from the current object field only not from related objects field values.

 

SpotInLine.Group_Leader_Line__r.Number_of_People_in_Line__c = 1;

 

 

This part is null 

SpotInLine.Group_Leader_Line__r

 

So when you try to fetch Number_of_People_in_Line__c  from ths null value it shows you attempt to deref a null object error.

 

There are certain other things also that i would like to tell you

 

1)You can not modify a trigger.old list item in before delete object it thorws you this error if you do it

System.FinalException: Record is read-only

 

Your trigger sould be like this

 

trigger LeavingTheLine on Place_in_Line__c (before delete)   {
     Set<ID> setGLLID = new Set<ID>(); 
     for (Place_in_Line__c SpotInLine : Trigger.old)
     {
         setGLLID.add(SpotInLine.Group_Leader_Line__c);          
SpotInLine.Group_Leader_Line__r.Number_of_People_in_Line__c = 1;    
     } 
   
   if(setGLLID.size() > 0)
    {
   //Change object API name if it is incorrect
   List<Group_Leader_Line__c> listGLL = [Select id from Group_Leader_Line__c where id in: setGLLID];
   for(Group_Leader_Line__c  obj : listGLL )
      {
          obj.Number_of_People_in_Line__c = 1;
      }
   
   update listGLL;

    }
}

 

All Answers

Shashikant SharmaShashikant Sharma

In your trigger , you are trying to access a related object's field directly from trigger.old list's item. Triger.old only can give you values from the current object field only not from related objects field values.

 

SpotInLine.Group_Leader_Line__r.Number_of_People_in_Line__c = 1;

 

 

This part is null 

SpotInLine.Group_Leader_Line__r

 

So when you try to fetch Number_of_People_in_Line__c  from ths null value it shows you attempt to deref a null object error.

 

There are certain other things also that i would like to tell you

 

1)You can not modify a trigger.old list item in before delete object it thorws you this error if you do it

System.FinalException: Record is read-only

 

Your trigger sould be like this

 

trigger LeavingTheLine on Place_in_Line__c (before delete)   {
     Set<ID> setGLLID = new Set<ID>(); 
     for (Place_in_Line__c SpotInLine : Trigger.old)
     {
         setGLLID.add(SpotInLine.Group_Leader_Line__c);          
SpotInLine.Group_Leader_Line__r.Number_of_People_in_Line__c = 1;    
     } 
   
   if(setGLLID.size() > 0)
    {
   //Change object API name if it is incorrect
   List<Group_Leader_Line__c> listGLL = [Select id from Group_Leader_Line__c where id in: setGLLID];
   for(Group_Leader_Line__c  obj : listGLL )
      {
          obj.Number_of_People_in_Line__c = 1;
      }
   
   update listGLL;

    }
}

 

This was selected as the best answer
WeylandYutanieWeylandYutanie

Thank you for the response.  I think my understanding of trigger.old and trigger.new isn't quite right.  If I want to access parameters from the object that caused the trigger, can I do something like the following?  Best regards, Weyland.

 

MyClass CurrentObj = trigger.old;

LIST<MyClass> AllObjs = 
    [Select Parameter1 From Place_in_Line__c Where Status = 'On'];

for (MyClass obj: AllObjs)
{
    if (obj.Parameter1 > CurrentObj.Parameter1)
        { Do something...}
}