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
manikanta sinisettimanikanta sinisetti 

Getting error when , I wrote a trigger --which should undelete child records when parent record is undeleted

Hi all,

This is my code:

//If Parent record is undeleted , it's child records should also  be undeleted.

trigger Trg9 on Student__c (after undelete) {
     List<id> StdId=new List<id>();
    for(Student__c s: trigger.new){
        StdId.add(s.id);
  
        }
    list<StudentChild__c> childRecords=[select id,College_of_Student__c,Name from  StudentChild__c where College_of_Student__c in :StdId all rows];
    undelete childRecords;
}

I am getting Error when Undeleing parent record:

User-added image

Please can any one help me out , how to resolve this.
Thanks In Advance.
Best Answer chosen by manikanta sinisetti
Jack Yu@TokyoJack Yu@Tokyo
Hi 
can you tell me What relation is between  Student__c  and StudentChild__c ? 
Lookup Relationship or  Master-Detail Relationship ?

If Relationship is 【Master-Detail Relationship】,
yes, your error will happens.  I got same error with you. if Relationship is 【Master-Detail Relationship】
you need not run trigger to undelete the child record, because it will undelete automaticaly.

If Relationship is 【Lookup Relationship】,
Yes, Child record can be undeleted  when Undeleing parent record:
I have two objects below, similar to you.
employee__c         ---> parent  object
employeeTask__c  ---> child object

------------------------------------------------------------------------------------
trigger Trg9 on employee__c (after undelete) {
    list<employeeTask__c> childRecords=[select id,employee__c,Name from  employeeTask__c where employee__c in :trigger.new all rows];
    system.debug('childRecords------------->'+ childRecords);
    system.debug('trigger.new------------->'+ trigger.new);
    undelete childRecords;
}
------------------------------------------------------------------------------------

User-added image

User-added image

I hope it will help you.
 

All Answers

VamsiVamsi
Hi,

Update the SOQL to include isdeleted = true 

Please mark as best answer if the above helps ..!!!
Jack Yu@TokyoJack Yu@Tokyo
Hi 
can you tell me What relation is between  Student__c  and StudentChild__c ? 
Lookup Relationship or  Master-Detail Relationship ?

If Relationship is 【Master-Detail Relationship】,
yes, your error will happens.  I got same error with you. if Relationship is 【Master-Detail Relationship】
you need not run trigger to undelete the child record, because it will undelete automaticaly.

If Relationship is 【Lookup Relationship】,
Yes, Child record can be undeleted  when Undeleing parent record:
I have two objects below, similar to you.
employee__c         ---> parent  object
employeeTask__c  ---> child object

------------------------------------------------------------------------------------
trigger Trg9 on employee__c (after undelete) {
    list<employeeTask__c> childRecords=[select id,employee__c,Name from  employeeTask__c where employee__c in :trigger.new all rows];
    system.debug('childRecords------------->'+ childRecords);
    system.debug('trigger.new------------->'+ trigger.new);
    undelete childRecords;
}
------------------------------------------------------------------------------------

User-added image

User-added image

I hope it will help you.
 
This was selected as the best answer