+ Start a Discussion

Trigger to delete multiple child object records when a parent record is deleted.

I have a parent object A and two child objects X, Y. Both X, Y have lookup relation to object A.

I'm looking for a trigger to delete related records in both X, Y objects when a parent record in object A is deleted.

Sampath SuranjiSampath Suranji
Hi ,
If you use Master detail relationship other than the lookup, when you deleting parent, child records automatically get deleted.
otherwise you can try below trigger 
trigger deleteRelatedChilds on ObjectA (before delete) {
    List<Id> ObjectAIds = new List<id>();
    for (ObjectA objAcc: System.Trigger.old) {
    List <X__C> xList=[select name from X__C where ObjectA__C in :accountIds] ;
    List <Y__C> yList=[select id from Y__C where ObjectA__C in :accountIds];
    delete xList;
    delete yList;


Best regards