+ Start a Discussion
karthikeya 2karthikeya 2 

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.
Best Answer chosen by karthikeya 2
Dayakar.DDayakar.D
Hi Karthlkeya,

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).
 
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;
}

Please let me know, if it helps you.

Best Regards,
Dayakar.D

All Answers

{!Piyush_soni__c}{!Piyush_soni__c}
hi Karthlkeya
use below trigger on parent object (in this trigger i use account as a parent and contact as a child object)
trigger DeleteContact on Account (after delete)
    {
        List<Contact> contacts = [SELECT AccountId FROM Contact WHERE AccountId IN:Trigger.OldMap.keyset()];
        delete contacts;
    }

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 :-)
 
Dayakar.DDayakar.D
Hi Karthlkeya,

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).
 
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;
}

Please let me know, if it helps you.

Best Regards,
Dayakar.D
This was selected as the best answer
ThejaTheja
Hi,

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
Renil RejithRenil Rejith
Hi ,Dayakar.D can you please trace ur code. I mean Explain how it works!! 

Thank U in advance 
DEEPALA SIVADEEPALA SIVA
Hi Karthikey,

can we do DML on before context?