You need to sign in to do that
Don't have an account?
bharath 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.
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; } }
Also try to add ALL ROWS in your order query, because orders are already in recycle bin and you wont get records in query.
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
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;
}
}
}