function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
shaker shaikhshaker shaikh 

parent record should be deleted

how to achive this--
if child record get deleteed then parent record also should get deleted in lookup relationship

 
Best Answer chosen by shaker shaikh
Nithesh NNithesh N
Just replace ParentId with Patient_Appointment__c. I assumed the lookup field on Child as ParentId. Sorry for not mentioning it as a post note.
Try this following code, and let me know if you are still facing the error.
 
trigger delete_child_ro_parent on Appointment__c (before delete) {  
   
if (Trigger.isDelete){  
       List<Appointment__c> AllChildRecords = [Select id, Patient_Appointment__c From Appointment__c Where Id IN  :Trigger.Old];
       
        List<Id> ParentIdList = New List<Id>();
        
        for(Appointment__c rec : AllChildRecords){
        
          ParentIdList.add(rec.Patient_Appointment__c);  
        }       
        delete [Select id from Patient__c Where Id IN :ParentIdList];
    }   
}
If it still doesn't work. Please Post the errors as it as.

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.

Best,
Nithesh
 

All Answers

steeve marshellsteeve marshell
Canon Printer Technical Support Toll-free Number 1-888-524-8675 for Toner Issue
if your canon printer is not perfectly working, slow issue, not able to print anything, connectivity  issue etc then quickly contact to canon printer technical support toll free number and get the quick assistance regarding Canon printer.
For more info:
http://www.apextm.com/support-for-canon-printer.php
Nithesh NNithesh N
Hi Shaker,
  • Create a before Delete Tigger
  • Get Child record Id from Trigger.Old 
  • Get ParentId from the child record after Querying the child record.
  • Query Parent record by the ParentId. 
  • Perform DML operation (delete) on that record.
This is the flow....

Best,
Nithesh
shaker shaikhshaker shaikh
Thank you , could plz you provide sample code
 
Nithesh NNithesh N
Can you provide me with Child Object and Parent Object API names and Also the ParentId field API Name in the Child Object? 
That way i can provide sample code for your config and use case.

Best,
Nithesh
shaker shaikhshaker shaikh
this is not project requirment actully, i always tried to do this but could not get correct code ..
lets assume same test name like
parent_obj__C
child_obj__C

 
Nithesh NNithesh N
This may work, Try it. I have reduced the steps as much as i can. 
 
trigger HelloWorld on child_obj__C (before delete) {   
    if (Trigger.isDelete){
       List<child_obj__C> AllChildRecords = [Select id, ParentId__c From child_obj__C Where Id IN  :Trigger.Old];
        List<Id> ParentIdList = New List<Id>();
        for(child_obj__C rec : AllChildRecords){
               ParentIdList.add(rec.ParentId__c);   
        }        
        delete [Select id from parent_obj__C Where Id IN :ParentIdList];
    }    
}
Let me know if it is working as you expected. 


Best,
Nithesh
 
shaker shaikhshaker shaikh
i tried it to apply it on objects as below

parent object - patient__C
child object - Appointment__C

 
child relationship name = Appointments

there is a field on (Appointment__C)   (Loopup relationship ) called "Patient_Appointment__c " on "Patient__C"

if receptionist (user)  delete appointment record patient record should get deleted this is requirment

i modified lated i replaceed your entire code with respective fields and obj name still it shows  error on - parentID fild and more errors 

trigger delete_child_ro_parent on Appointment__c (before delete) {  
   
if (Trigger.isDelete){
    
       List<Appointment__c> AllChildRecords = [Select id, ParentId From Appointment__c Where Id IN  :Trigger.Old];
       
        List<Id> ParentIdList = New List<Id>();
        
        for(Appointment__c rec : AllChildRecords){
        
          ParentIdList.add(rec.ParentId);  
        }       
        delete [Select id from Patient__c Where Id IN :ParentIdList];
    }   
}

please let me know if need any information about object ...
Nithesh NNithesh N
Just replace ParentId with Patient_Appointment__c. I assumed the lookup field on Child as ParentId. Sorry for not mentioning it as a post note.
Try this following code, and let me know if you are still facing the error.
 
trigger delete_child_ro_parent on Appointment__c (before delete) {  
   
if (Trigger.isDelete){  
       List<Appointment__c> AllChildRecords = [Select id, Patient_Appointment__c From Appointment__c Where Id IN  :Trigger.Old];
       
        List<Id> ParentIdList = New List<Id>();
        
        for(Appointment__c rec : AllChildRecords){
        
          ParentIdList.add(rec.Patient_Appointment__c);  
        }       
        delete [Select id from Patient__c Where Id IN :ParentIdList];
    }   
}
If it still doesn't work. Please Post the errors as it as.

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps address your issue.

Best,
Nithesh
 
This was selected as the best answer
shaker shaikhshaker shaikh
Thanks Nithesh
i took reference of provided code but still i shows error ' Variable does not exist: AllChildRecords  '

List<Appointment__c> AllChildRecords = [SELECT id,Patient__c from Appointment__c where id IN :Trigger.old ];
      }
    set<Id> parentIDlist = new set<id>();
    for(Appointment__c ap : AllChildRecords){
        parentIDlist.add(ap.Patient__c);

i think it should iterate(AllChildRecords ) in for loop code looks fine but not allowing 
 
Nithesh NNithesh N
I can see a curly brace after the List<Appointment__c> AllChildRecords, 
Which means, You made AllChildRecords belongs to different block of code ( a local variable), So, you can't access it outside the block. 

Make the List<Appointment__c> AllChildRecords as Global variable. That will work. or Bring it out of Code block. 
Post the full code, so that i can make changes appropriately. Try it. and let me know if it still doesn't work.

Best,
Nithesh
 
shaker shaikhshaker shaikh
i am trying this code...

trigger delete_child_ro_parent on Appointment__c (before delete) {  
   
if (Trigger.isDelete){
    
       List<Appointment__c> AllChildRecords = [Select id, ParentId From Appointment__c Where Id IN  :Trigger.Old];
       
        List<Id> ParentIdList = New List<Id>();
        
        for(Appointment__c rec : AllChildRecords){
        
          ParentIdList.add(rec.ParentId);  
        }       
        delete [Select id from Patient__c Where Id IN :ParentIdList];
    }   
}
Victor ThomosVictor Thomos
Upcoming ford cars in India 2020 is an important milestone in the history of the company, as the most important model of the brand is introduced. The Ford Mustang GT has gained incredible popularity in India.
Astro ShikshaAstro Shiksha
After the completion of this Online Astrology course in India, you’ll be able to understand and solve others’ life problems. The online Astrology course at Astro Shiksha will make you learn various concepts.