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
HamptonHampton 

Simple Trigger To Delete Records

Hello:

 

I am attempting to delete a group of child records if a parent record is updated. I cannot seem to get the trigger to fire. Any thoughts? Am I over-complicating this?

 

When a Turf__c record is updated, I want to delete all associated Rep_Summary__c records.

 

trigger RepAssignmentDelete on Video_Turf__c (after update) {

    Set<ID> turfDoors = new Set<ID>();
    for(Video_Turf__c turfRep : trigger.old)
    (turfDoors.add(turfRep.ID));
    
    Map<String, Video_Turf__c> turfMap = new Map <String, Video_Turf__c>();
    for(Video_Turf__c turf1 : [Select ID, Name, Date__c, Doors_Per_Rep__c from Video_Turf__c where ID in : turfDoors])
    turfMap.put(turf1.Name, turf1);
    
    List<Rep_Summary__c> repsToDelete = new List<Rep_Summary__c>();
    for(Rep_Summary__c a :[Select Name, Date__c, Doors__c, Release__c from Rep_Summary__c where Release__c in : turfDoors])
    if(turfMap.containskey(a.Release__c)){
        repsToDelete.add(a);
    }
delete repsToDelete;
}

 Thanks,

 

Hampton

Best Answer chosen by Admin (Salesforce Developers) 
Marko LamotMarko Lamot
trigger RepAssignmentDelete on Video_Turf__c (after update) {

Set<ID> turfDoors = new Set<ID>();
for(Video_Turf__c turfRep : trigger.old)
 (turfDoors.add(turfRep.ID));

List<Rep_Summary__c> repsToDelete = new List<Rep_Summary__c>();
repsToDelete = [Select id from Rep_Summary__c where Release__c in : turfDoors]);
if (repsToDelete.size()>0) delete repsToDelete
}

All Answers

Marko LamotMarko Lamot
trigger RepAssignmentDelete on Video_Turf__c (after update) {

Set<ID> turfDoors = new Set<ID>();
for(Video_Turf__c turfRep : trigger.old)
 (turfDoors.add(turfRep.ID));

List<Rep_Summary__c> repsToDelete = new List<Rep_Summary__c>();
repsToDelete = [Select id from Rep_Summary__c where Release__c in : turfDoors]);
if (repsToDelete.size()>0) delete repsToDelete
}
This was selected as the best answer
HamptonHampton

You rock, thanks!