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
adi salesforceadi salesforce 

Undelete the record which is deleted.

Glyn Anderson (Slalom)Glyn Anderson (Slalom)
This will undelete all deleted Accounts:

undelete [SELECT Id FROM Account WHERE IsDeleted = true ALL ROWS];
Ajay K DubediAjay K Dubedi
Hi Adi,

Please try this code and check all previous questions which you have the post.
mark as solve which may help others.

trigger UnDeleteRecordTrigger on Account (after undelete) {
    if(Trigger.isAfter && Trigger.isUndelete) {    
        List<Account> accListToUpdate = new List<Account>();
        Set<Id> accidset = new Set<Id>();
        for(Account acc: Trigger.New){
        system.debug('accidset--> '+accidset);
        List<Account> accNewList = [Select Id, Name From Account Where Id=:accidset limit 1];
        for(Account ac: accNewList){
            ac.Name = 'Undeleted'+ ac.Name;
        update accListToUpdate;

Please let me know if you have any query.
Please mark it as best Answer if you find it helpful.

Thank You
Ajay Dubedi
Hi Adi,

You can try the following query to undelete the record from Recycle bin:
List<Account> lstAcc = [SELECT Id FROM Account WHERE IsDeleted = true];
system.debug('Deleted Record list is'+lstAc);
undelete lstAcc;

I did not use ALL ROWS here in query, since ALL ROWS pull all the deleted record from the system but the records only from recycle bin can be un deleted, rest you can not un delete though query returns them.

Mark answer as solved if does help you.
Hi Adi,

Try this code:
Account a = new Account();
  a.Name='Universal Containers';
   Contact c = new Contact();
   delete a;

Account[] savedAccts = [SELECT Id, Name FROM Account WHERE Name = 'Universal Containers' ALL ROWS]; 
try {
} catch (DmlException e) {
    // Process exception here

Please make it best if it helps you.


Naveen DhanarajNaveen Dhanaraj
If you want to undelete specific records use Recycle bin to undelete or you can run above query in Execute anonymous window with specfic where conditions like deleted by, deleted date,name..

User-added image