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
Jay WeerappuligeJay Weerappulige 

Trigger to delete records on two objects

I have written a trigger to delete records on two objects after updating on parent object.
This is how relationship
Training_Plan__c (Parent) to STM_Computation__c (Child)
Monthly_Totals__c (Parent) to STM_Computation__c (child)
After updating on Training_Plan__c records on STM_Computation__c needs to be deleted. Any Monthly_Totals__c records associated with deleted STM_Computation__c records need to be deleted as well. But my trigger delete only STM_Computation__c. It doesn't delet associated Monthly_Totals__c records.
My trigger:

trigger STMdeleteFromWithdrawal on Training_Plan__c (After update) { Set<Id> ids = Trigger.newMap.keySet(); List<Training_Plan__c> updatedTAs = [Select Id, TEC_Status__c, (select ID, STM_Date__c, Training_Plan_Withdrawal_Date__c, Monthly_Totals_Name__c from STM_Computations__r) From Training_Plan__c where Id IN :ids]; //get collection of STM computaion records to delete List<STM_Computation__c> stmToDelete = new List<STM_Computation__c>(); for (Training_Plan__c tps : updatedTAs){ String oldValue = Trigger.oldMap.get(tps.Id).TEC_Status__c; String newValue = tps.TEC_Status__c; if(oldValue != newValue){ for (STM_Computation__c stm: tps.STM_Computations__r){ if(stm.STM_Date__c>stm.Training_Plan_Withdrawal_Date__c){ stmToDelete.add(stm); } } } } //get collection of Monthly Total ids to delete set<id> mtsIds = new set<id>(); for (STM_Computation__c stms : stmToDelete){ mtsIds.add(stms.Monthly_Totals_Name__c); } List<Monthly_Totals__c> monthlyTotalsToDelete = [Select Id From Monthly_Totals__c where Id IN :mtsIds]; //Delete STM Computation records if(!stmToDelete.isEmpty()) { delete stmToDelete; } //Delete Monthly Totals records if(!monthlyTotalsToDelete.isEmpty()) { delete monthlyTotalsToDelete; } }
Jay WeerappuligeJay Weerappulige
trigger STMdeleteFromWithdrawal on Training_Plan__c (After update) {
    
    Set<Id> ids = Trigger.newMap.keySet();
    List<Training_Plan__c> updatedTAs = [Select Id, TEC_Status__c, (select ID, STM_Date__c, 
                                                                    Training_Plan_Withdrawal_Date__c,
                                                                    Monthly_Totals_Name__c 
                                                                    from STM_Computations__r)
                                         From Training_Plan__c
                                         where Id IN :ids];    
    //get collection of STM computaion records to delete
    List<STM_Computation__c> stmToDelete = new List<STM_Computation__c>();
    
    for (Training_Plan__c tps : updatedTAs){
        String oldValue = Trigger.oldMap.get(tps.Id).TEC_Status__c;
        String newValue = tps.TEC_Status__c;
        
        if(oldValue != newValue){
            for (STM_Computation__c stm: tps.STM_Computations__r){
                if(stm.STM_Date__c>stm.Training_Plan_Withdrawal_Date__c){
                    stmToDelete.add(stm);
                }
            }
        }
    }
    
    //get collection of Monthly Total ids to delete
    set<id> mtsIds = new set<id>();
    for (STM_Computation__c stms : stmToDelete){
        mtsIds.add(stms.Monthly_Totals_Name__c);
    }
    
    List<Monthly_Totals__c> monthlyTotalsToDelete = [Select Id From Monthly_Totals__c
                                                     where Id IN :mtsIds];    
    //Delete STM Computation records  
    if(!stmToDelete.isEmpty())
    {
        delete stmToDelete;
    }
    //Delete Monthly Totals records   
    if(!monthlyTotalsToDelete.isEmpty())
    {
        delete monthlyTotalsToDelete;
    }
}
GarryPGarryP
The trigger logic looks fine.
Did you check
1. Any afterDelete trigger on Sm computation record is doing something which prevents deletion of Monthly records?
2. Are there any exception in Sm computation triggers, if any?