Wednesday 26 October 2022

How to restore delete records using trigger

Undelete operation in the trigger will be fired, if you are restoring deleted record from the recycle bin. Example. If you deleted an account record, then it will be moved to recycle bin, if you restore the deleted account record from recycle bin, then undelete event in account object will get triggered. deleted data is stored in the recycle bin for 15 days.


trigger AccountUndeleteTrigger on Account (after undelete)  {    

 List<Account> listAccountToUpdate = new List<Account>();    

 for(Account acc : trigger.new)     {             

 acc.Name = 'Undeleted:' + acc.Name;            

 listAccountToUpdate.add(acc);     

}          

 if(listAccountToUpdate.size() > 0 )    

 {         

update listAccountToUpdate;     

}      

}