0

I'm new to Apex and code. I have 2 objects that are linked to each other 1 to 1. There is a lookup to the other record on each record.

I'm writing a trigger that, when record A is deleted, record B is also deleted.

Here's my code:

trigger TateTimeEntryNewTrigger on Time_Entry_New__c (before delete) {
List<Id> idsToQuery = new List<Id>{};
for(Time_Entry_New__c ten : Trigger.new) {
    idsToQuery.add(ten.Id);
}

SVMXC__Service_Order_Line__c[] wdsToDelete = [SELECT Id FROM SVMXC__Service_Order_Line__c WHERE Time_Entry_New__c.Id IN :idsToQuery];

delete wdsToDelete;}

When I delete record A (Time_Entry_New), I get this error:

System.NullPointerException: Attempt to de-reference a null object: Trigger.TateTimeEntryNewTrigger: line 3, column 1"

I of course found this code from other examples. I'm thinking it's not necessary to have a list since it's always 1 record? Any thoughts on fixing the error and cleaning up the code will be very appreciated.

Thanks

1 Answer 1

7

This is happening because Trigger.new is not available on before delete context. You'll want to use Trigger.old instead.

Check this document about Trigger Context Variables.

2
  • That did it, thank you! Changing that also uncovered a second problem that my dot notation needed to be Time_Entry_New__r.Id. Commented Nov 7, 2017 at 19:23
  • 1
    Actually you can just use Time_Entry_New__c. The relationship field ended in __c is interpreted as an Id. __r.Id also works, but it isn't optimal (because you are referring to the object's relationship data instead of the object's data itself). Commented Nov 7, 2017 at 19:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .