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
Rakhi B 10Rakhi B 10 

How to restore delete records using trigger

SandhyaSandhya (Salesforce Developers) 
Hi,

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
 
Ajay K DubediAjay K Dubedi
Hi Rakhi,

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.
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;
	}
	
}

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
Rakhi B 10Rakhi B 10
Hi Ajay Dubedi, Trigger is not Working!!. if we delete account record. after trigger using to Restore that Record.. Thanks Rakhi
Rakhi B 10Rakhi B 10
Hi, How to undelete the Records using trigger.. Thanks Rakhi
Ajay K DubediAjay K Dubedi
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;
    }
}

Please let me know if you have any query.

Thanks 
Ajay Dubedi
manu manu 23manu manu 23
Is it possible to undelete bulk of records using triggers?