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.
The after undelete trigger event only works with recovered records—that is, records that were deleted and then recovered from the Recycle Bin through the undelete DML statement. These are also called undeleted records.
//Trigger class
trigger AccountUndeleteTrigger on Account (after undelete)
{
if(Trigger.isAfter && Trigger.isUndelete) {
undeleteRecord.isUndelete(trigger.new);
}
}
//Helper Class
public class undeleteRecord {
public static void isUndelete(List<Account> accList){
system.debug('acclist--> '+acclist);
List<Account> accListToUpdate = new List<Account>();
Set<Id> accidset = new Set<Id>();
for(Account acc: accList){
accidset.add(acc.Id);
}
system.debug('accidset--> '+accidset);
List<Account> accNewList = [Select Id, Name From Account Where Id=:accidset];
for(Account ac: accNewList){
ac.Name = 'Undeleted'+ ac.Name;
accListToUpdate.add(ac);
}
update accListToUpdate;
}
}
Refer below link which has the sample code for the same.
http://www.force2b.net/index.php/2009/03/logging-deleted-records-in-salesforcecom/
Please mark it as Solved if my reply was helpful. It will make it available for other as the proper solution.
Thanks and Regards
Sandhya
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.
I would suggest you to go thorugh below two links to get more grasps on the same.
https://developer.salesforce.com/page/Trigger_Frameworks_and_Apex_Trigger_Best_Practices.
http://amitsalesforce.blogspot.com/2015/10/trigger-context-variables.html
Thanks
Ajay Dubedi
Please let me know if you have any query.
Thanks
Ajay Dubedi