trigger TriggerTo_Delete_ChildRecords on Account (before delete) {
//To store parent ids list<id> AccountIds=new list<id>(); for(Account accountVar:trigger.old) { AccountIds.add(accountVar.id); } //Collecting all child records related to Parent records list<contact> listOfContacts=[select id from Contact where accountid in :AccountIds]; system.debug('listOfContacts'+listOfContacts); //deleting child records delete listOfContacts; }
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
You can try following code:
Let me know if you are getting any error.
Thanks,
Bhavesh
PFB
Regards
Rajesh
Please try this code:
trigger TriggerTo_Delete_ChildRecords on Account (before delete) {
//To store parent ids
list<id> AccountIds=new list<id>();
for(Account accountVar:trigger.old)
{
AccountIds.add(accountVar.id);
}
//Collecting all child records related to Parent records
list<contact> listOfContacts=[select id from Contact where accountid in :AccountIds];
system.debug('listOfContacts'+listOfContacts);
//deleting child records
delete listOfContacts;
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha