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
bittu myanabittu myana 

how solve the fallowing case study Using Batch class Deleted records should be undeleted and update in the Account object

NagendraNagendra (Salesforce Developers) 
Hi Bittu,

Firstly may I request you to please elaborate your requirement so that we can understand better.Assuming if a record is deleted from account object it resides in recycle bin.If it is undeleted using batch apex class it automatically gets restored in account object, again why you want to update the records in account object?

Please find the sample code to undelete the records using batch apex and tweak it as per your requirement which might help you.
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);        
    }
}
Hope this helps.

Regards,
Nagendra.


 
bittu myanabittu myana
Hi Nagendra 
I have tried that code but i am unabale solve the problem.the batch is failed and the records are not displaying even in the query part from recycyle bin.

 
bittu myanabittu myana
Hi Nagendra,
my requirement is the account records which are deleted has to get back to the account object.