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
fourfourfunfourfourfun 

Deleting Related Objects trigger

Hi,

 

Got two objects. When saving on one, a trigger creates a bunch on the other referecing back with a lookup. When I delete from the former, I want it to delete everything that it created when saving.

 

This is what I have so far:

 

trigger CleanRelatedPCMActivities on Week__c (before delete) 

{
set<id> setActIds = Trigger.newmap.keySet();
 
//query for related Activities
 
list<PCM_Time_Record__c> ActivityRecords = [select Id from PCM_Time_Record__c where Weekly_Time__c in:setActIds];
if(ActivityRecords.size() > 0)
delete ActivityRecords;
}

Theory sounds good. Before deleting, set the ID of what you are deleting. Then query in the other object to find what it is related to, deleting whatever is found.

 

This is giving me this error though:

 

execution of BeforeDelete caused by: System.NullPointerException: Attempt to de-reference a null object: Trigger.CleanRelatedPCMActivities: line 4, column 1"

 

Anyone got a pointer on where I may have gone wrong?

Best Answer chosen by Admin (Salesforce Developers) 
SeAlVaSeAlVa

Hi, 

 

your trigger is a "before delete", what means that there is no "trigger.new"

 

use this line instead

Set<ID> setActIds = Trigger.oldMap.keySet();

 Regards

All Answers

SeAlVaSeAlVa

Hi, 

 

your trigger is a "before delete", what means that there is no "trigger.new"

 

use this line instead

Set<ID> setActIds = Trigger.oldMap.keySet();

 Regards

This was selected as the best answer
fourfourfunfourfourfun

Stunning. 3 little letters.

 

Thanks!