+ Start a Discussion
venkatsforcevenkatsforce 

After Delete trigger functionality

i want to delete a child record when parent record is delete after a trigger fires........example plzzzzzzz

Best Answer chosen by Admin (Salesforce Developers) 
venkatsforcevenkatsforce
 

if(Trigger.isDelete)

{

   Set setdeleteId = new Set();

   for(Contact delc : trigger.old)

  {

   setdeleteId.add(delc.Id);

    List delcont =[Select Id,Contact__c from CustomDetails__c where Contact__c=' '] ;

    delete delcont;

   }

 

}

All Answers

Vinit_KumarVinit_Kumar

What kind of relationship you have,if it is a Master Detail it would delete the child records automatically.

SFAdmin5SFAdmin5

the below works assuming it's not a m-d relationship, in which case you don't need any code

 

trigger deleteChildrenOnParentDelete on Parent_Object__c (before delete) 
{   
        List<Parent_Object__c> parentList = new List<Parent_Object__c>([SELECT Id FROM Parent_Object__c]);                 
        Map<Id,Parent_Object__c> childMap = new Map<Id,Parent_Object__c>();
            For(Parent_Object__c c: parentList)
            {
                childMap.put(c.ID,c);
            } 
            
        List<Child_Object__c> childList = new List<Child_Object__c>();
            For(Child_Object__c c: [select Id,Parent_Object__c from Child_Object__c where Parent_Object__c IN: childMap.Keyset()])
            {
                childList.add(c);
            } 
                Delete childList;                             
}

 

venkatsforcevenkatsforce

i use the lookup relationship for parent-child.......

venkatsforcevenkatsforce
 

if(Trigger.isDelete)

{

   Set setdeleteId = new Set();

   for(Contact delc : trigger.old)

  {

   setdeleteId.add(delc.Id);

    List delcont =[Select Id,Contact__c from CustomDetails__c where Contact__c=' '] ;

    delete delcont;

   }

 

}

This was selected as the best answer