You need to sign in to do that
Don't have an account?
GauravTrivedi
How to undelete records using Apex Batch Class.
Hi,
I was trying to recover record which are accedently deleted from salesforce instance and I am using the following code:
When I am trying to execute my batch class it doesnt undelete records from recycle bin. Can anyone please help me out. Thanks!
I was trying to recover record which are accedently deleted from salesforce instance and I am using the following code:
global class BatchClass implements Database.Batchable<sObject>,Schedulable{ global Database.queryLocator start(Database.BatchableContext bc){ String query = 'SELECT Id, Name, IsDeleted, ItemName__c FROM Buffer__c WHERE IsDeleted = True ALL ROWS'; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<Buffer__c> scope){ List<Buffer__c> bufferList = new List<Buffer__c>(); for(Buffer__c s:scope){ s.ItemName__c='Gaurav'; bufferList.add(s); } system.debug('Data--->'+bufferList); undelete bufferList; } global void finish(Database.BatchableContext bc){ AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email FROM AsyncApexJob WHERE Id =:BC.getJobId()]; Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] names = new String[]{a.CreatedBy.Email}; mail.setToAddresses(names); mail.setSubject('Mail: Test'); mail.setPlainTextBody('Hi ,\n\nThe following job is '+a.Status+' and jobId is '+a.Id+'\n\n\n Regards,\n'+a.CreatedBy.Email); Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail}); } global void execute(SchedulableContext s){ Database.executeBatch(this); } }
When I am trying to execute my batch class it doesnt undelete records from recycle bin. Can anyone please help me out. Thanks!
All Answers
Let's take it apart (outside of batch) and confirm that is working first. I am just typing pseudo-code below outside the IDE
We essentially just need to make sure the undelete is happening as you expect first then we can move onto batch. Note - just adjust the query to limit the number of rows you get back like you have done above where you only add if ItemName__c = 'Gaurav'
I tried both ways.
When I am executing the query outside start method it is returning result, but when using inside Start it is giving null.
Notice I just commented out the scheduleable stuff and I am just kicking this thing off on the fly in Eclipse with the Developer Console open and hoping over to look at the logs.
From everything I can see this stripped down version works properly in batch apex. Here are all the files (including the batch class of yours that I commented some code out of and how I kick it off to test it.)
http://www.salesforcegeneral.com/storage/OrginalTestOnDeleted_Buffer__c.png (http://www.salesforcegeneral.com/storage/OrginalTestOnDeleted_Buffer__c.png" target="_blank)
http://www.salesforcegeneral.com/storage/BatchClass-StartMethod-ReturnsTwoDeletedRecords.png
http://www.salesforcegeneral.com/storage/BatchClass-ExecuteAnonymous.png
http://www.salesforcegeneral.com/storage/BatchClass.cls
http://www.salesforcegeneral.com/storage/DebugLogs-LookGood.png
To kick off the stipped down batch class I just use Eclipse or the Dev Console to Execute Anonymous Apex with the following command
http://www.salesforcegeneral.com/storage/BatchClassUndeleteTesting.zip
Hello Lars,
I am trying to retrieve each record that was deleted including the records of my custom object's records and standard object records, How can I acheive that?
public class BatchApexJob Implements Database.Batchable<sObject>{
public List<sObject> Start(Database.BatchableContext Bc)
{
List<Flight__c> FL = [SELECT Id,IsDeleted FROM Flight__c WHERE IsDeleted = True ALL ROWS];
return FL;
}
public void execute(Database.BatchableContext Bc,List<sObject> FD){
undelete FD;
}
public void Finish(Database.BatchableContext Bc){
}
}