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
bharath kumar 52bharath kumar 52 

Trigger to restore child record after undeleting parent is not working

Hi All,

I am working on a scenario where i have 2 objects : Accounts and Order1__c(order1 has a lookup relationship with account).
On deletion of account i am able to delete the related child records. But on undelete i am getting the error.

On undelete
Below is my implementation for the same.

trigger OrderDeletion on Account (before delete,after delete,after undelete) {
    
List<Id> AccIds= new List<Id>();
    
    for(Account acc: trigger.old){
       
        
        AccIds.add(acc.id);
    }
    List<Order1__c> ordlist=[select id,name,Account__c from Order1__c WHERE Account__c in:AccIds ];
    if(ordlist.size()>0)
    delete ordlist;
    
    if(trigger.isAfter && trigger.isUndelete){
        if(ordlist.size()>0)
        undelete ordlist;
        
    }
}
 


 

AshishkAshishk
After undelete can only access Trigger.New and not Trigger.Old, I think becuase of it you are getting this error.

Also try to add ALL ROWS in your order query, because orders are already in recycle bin and you wont get records in query.
sfdcMonkey.comsfdcMonkey.com
Hi bharth, use below code 
trigger OrderDeletion on Account (before delete,after undelete) {
    
    List<Id> AccIds= new List<Id>();
   
    if(trigger.isbefore && trigger.isdelete){  
        for(Account acc: trigger.old){
            AccIds.add(acc.id);
        }
        List<Order1__c> ordlist=[select id,name,Account__c from Order1__c WHERE Account__c in:AccIds];
        if(ordlist.size()>0){
            delete ordlist;
        }
    }
    
    if(trigger.isAfter && trigger.isUndelete){
        for(Account acc: trigger.new){
            AccIds.add(acc.id);
        }
        
        List<Order1__c> ordlist2=[select id,name,Account__c from Order1__c WHERE Account__c in:AccIds AND isDeleted = true ALL ROWS];
        if(ordlist2.size()>0){
            undelete ordlist2;
        }
        
    }
}

i hope it helps you.
  Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
thanks 
sfdcmonkey.com 
Harshavardhan Reddy 29Harshavardhan Reddy 29
Hi I want to delete custom object records and also case records when account is deleted using trigger. Undelete the child records if account is undeleted the trigger works only for custome objects but case records are not deleted and undelted .Below is the trigger code.

trigger OrderDeletion on Account (before delete,after undelete) {
    
    List<Id> AccIds= new List<Id>();
   
    if(trigger.isbefore && trigger.isdelete){  
        for(Account acc: trigger.old){
            AccIds.add(acc.id);
        }
        List<AccountOrder__c> ordlist=[select id,name,AccountOrder__c from AccountOrder__c WHERE AccountOrder__c in:AccIds];
        if(ordlist.size()>0){
            delete ordlist;
        }
        
        List<Case> caselist = [select id from Case where Account.Id in:AccIds];
        if(caselist.size()>0){
            delete caselist;
        }
    }
    
    if(trigger.isAfter && trigger.isUndelete){
        for(Account acc: trigger.new){
            AccIds.add(acc.id);
        }
        
        List<AccountOrder__c> ordlist2=[select id,name,AccountOrder__c from AccountOrder__c WHERE AccountOrder__c in:AccIds AND isDeleted = true ALL ROWS];
        if(ordlist2.size()>0){
            undelete ordlist2;
        }
        
         List<Case> caselist1 = [select id from Case where Account.Id in:AccIds AND isDeleted = true ALL ROWS];
        if(caselist1.size()>0){
            delete caselist1;
        }
    }
}
Vishnu SanthoshVishnu Santhosh
ALL ROWS was the problem for me. Thanks @Ashishk 👍