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
gowtham murugesangowtham murugesan 

hi all,Please give me test class for this

My below  batch class is working fine,kindly give me test class for this,its totally new for me.

global class AccountUndeleteBatchApex implements Database.Batchable<sObject>{
    global Database.queryLocator start(Database.BatchableContext bc){
        String query = 'SELECT Id,Name FROM Account WHERE     IsDeleted = True ALL ROWS ';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext bc, List<Account> scope){
        List<Account> accList = new List<Account>();
        list<Deleted_Record_ID__c> DelRe = new list<Deleted_Record_ID__c>();
        for(Account s:scope){
            accList.add(s);
        }
        System.debug('account list' + accList );
        for(Account ac:accList){
            Deleted_Record_ID__c dr=new Deleted_Record_ID__c();
            dr.Name = ac.id;
            dr.Name__c = ac.Name;
            DelRe.add(dr);
        } 
        insert DelRe;
        Undelete accList;
    }
    global void finish(Database.BatchableContext bc){
    }
}
Best Answer chosen by gowtham murugesan
Khan AnasKhan Anas (Salesforce Developers) 
Hi Gowtham,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine.
 
@isTest
public class Test_BatchClass {
    
    static testMethod void testMethod1(){
        Account a = new Account(Name='Khan');
        INSERT a;
        
        DELETE a;
        
        Account[] delAccts = [SELECT Id, Name FROM Account WHERE Name = 'Khan' ALL ROWS]; 
        
        Deleted_Record_ID__c did = new Deleted_Record_ID__c();
        for(Account acc : delAccts){
        	did.Name = acc.Id;
        	did.Name__c = a.Name;
        }
        INSERT did;
        
        Test.startTest();
        
        Batch_UndeleteAndInsert obj = new Batch_UndeleteAndInsert();
        DataBase.executeBatch(obj); 
        
        Test.stopTest();
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas