+ Start a Discussion
ch ranjithch ranjith 

when parent deleted child should delete Please help in this code

trigger master on parent__c (after delete)
{
  set<id> set1=new set<id>();
  list<child__C> chdellist=new list<child__C>();
  for(parent__c p:trigger.old)
  {
   set1.add(p.id);
  }
  list<child__c> chlist=[select id,name,ref_parent__c from child__c where ref_parent__c in:set1];
  system.debug(chlist);
  for(parent__c p:trigger.old)
  {
  for(child__C c:chlist)
  {
  if(p.id==c.ref_parent__c)
     system.debug(p.name+'  '+c.name);
  {
   chdellist.add(c);
  }
  }
  }
  delete chdellist;
  }
Best Answer chosen by ch ranjith
ch ranjithch ranjith
trigger masterdelete on parent__c (before delete)
{
list<parent__C> pnewlist=new list<parent__C>();
   set<id> set1=new set<id>();
    for(parent__C p:trigger.old)
    {
     set1.add(p.id);
    }
    list<child__C> chlist=[select id,name,ref_parent__c from child__C where ref_parent__c in:set1];
  
delete chlist;
}

All Answers

Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
Hi Ranjith,

You can go for master detail relationship between two objects .why you need a trigger?
ch ranjithch ranjith
Hi vidhyasagarran,

I need three master detail relationship in one object so i one more master detail relationship apart from salesforce standard functionlaity so i was created one look up with the help triggers its should behave like master

so please help me
Vidhyasagaran MuralidharanVidhyasagaran Muralidharan
trigger master on parent__c (after delete)
{
list<child__C> chdellist=new list<child__C>();
for(parent__c p:trigger.old)
{
list<child__c> chlist=[select id,name,ref_parent__c from child__c where ref_parent__cin:p.id];
if(chlist.size()>0)
{
chdellist.add(chlist);
}
delete chdellist;
}
this will wok i guess
ch ranjithch ranjith
It works but i need to write soql query outside the for loop. I tried this but not working 

trigger masterdelete on parent__c (after delete)
{
list<parent__C> pnewlist=new list<parent__C>();
   set<id> set1=new set<id>();
    for(parent__C p:trigger.old)
    {
     set1.add(p.id);
    }
    list<child__C> chlist=new list<child__C>();
   list<parent__c> plist=[select id,name,(select id,name from Parent__c.childs__r) from parent__c where id in:set1];
   for(parent__c p1:plist)
   {
    for(child__C c1:p1.childs__r)
    {
     chlist.add(c1);
     }
}
delete chlist;
}
Jen BennettJen Bennett
See example on: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_bulk_idioms.htm
You can create your child list using a soql query from the parent keyset and eliminate the for loops:
List<child__c> chlist = new List<child__c>([SELECT Id, Name FROM child__c WHERE parent__c IN :trigger.oldMap.keySet();
ch ranjithch ranjith
ch ranjith
trigger masterdelete on parent__c (after delete)
{
list<parent__C> pnewlist=new list<parent__C>();
   set<id> set1=new set<id>();
  List<child__c> chlist = new List<child__c>([SELECT Id, Name FROM child__c WHERE ref_parent__c IN :trigger.oldMap.keySet()]);
system.debug(chlist);
delete chlist;
}
The child records is not retrieving in tge chlist why?
Help me soon
Jen BennettJen Bennett
I think because it's after delete and you probably have the lookup set to Clear the value of this field. You can't choose this option if you make this field required. 
ch ranjithch ranjith
jjbennett530@Please tel me any some other way to this 
Jen BennettJen Bennett
try before delete vs after delete
ch ranjithch ranjith
Before delete also not working....
ch ranjithch ranjith
trigger masterdelete on parent__c (before delete)
{
list<parent__C> pnewlist=new list<parent__C>();
   set<id> set1=new set<id>();
    for(parent__C p:trigger.old)
    {
     set1.add(p.id);
    }
    list<child__C> chlist=[select id,name,ref_parent__c from child__C where ref_parent__c in:set1];
  
delete chlist;
}
This was selected as the best answer
Jen BennettJen Bennett
I just tested it out in my dev org and seems to work fine. Are you sure that it's a lookup field and that you are referencing the correct parent field and that when you are testing the parent has children and you are deleting the parent record. What do you see in the debug log? My trigger:
trigger masterDelete on Foo__c (before delete) {
 
    List<Whatever__c> chlist = new List<Whatever__c>([SELECT Id, Name FROM Whatever__c WHERE foo__c IN :trigger.oldMap.keySet()]);
    system.debug(chlist);
    delete chlist;

}