You need to sign in to do that
Don't have an account?

Trigger for deleting child records when parent is deleted.
Hi All
can any one help in writter a trigger,
when parent is deleted the related child records should be deleted.
Thanks in advance.
can any one help in writter a trigger,
when parent is deleted the related child records should be deleted.
Thanks in advance.
Please find below code, Here I am taking account and contact Parent child relation ( you can take your require object in place of account and contacts).
Please let me know, if it helps you.
Best Regards,
Dayakar.D
All Answers
use below trigger on parent object (in this trigger i use account as a parent and contact as a child object)
If your have custom object relationship means use Lookup_Field__r.Id in the place of AccountId in the above query.
Thanks
Please Mark it best answer if it helps you so it make proper solution for others in future :-)
Please find below code, Here I am taking account and contact Parent child relation ( you can take your require object in place of account and contacts).
Please let me know, if it helps you.
Best Regards,
Dayakar.D
Can anyone help me how to write a trigger for update and delete operation for custom objects without any relation (Ex: Parent__c & child__c. But no relationship between them) ? Below is my code
trigger ChildInsert on Parent__c (after insert,after update) {
List<child__c> childs = new List<child__c>();
//Insert Operation
for(Parent__c Par : Trigger.new)
{
childs.add(new child__c(Name_del__c = Par.Name__c,Email__c = Par.Email__c , Phone__c = Par.Phone__c));
}
insert childs;
}
//Update Operation
for(Parent__c Par : Trigger.old)
{
childs.add(new child__c(Name_del__c = Par.Name__c, Email__c = Par.Email__c , Phone__c = Par.Phone__c));
}
update childs;
/*
// Delete Operation
list<id> ParentIds=new list<id>();
for(Parent__c Par : trigger.old)
{
ParentIds.add(Par.id);
}
//Collecting all child records related to Parent records
list<child__c> listOfChilds=[select id from child__c where Name IN: Par.Name];
system.debug('listOfChilds'+listOfChilds);
//deleting child records
delete listOfChilds;
*/
Thanks
Theja
Thank U in advance
can we do DML on before context?