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
Eric Blaxton 11Eric Blaxton 11 

test class for UnlockRecordBatchJob implements Database.batchable

Hi and thanks in advance...

I found this class online without Test class.  The class works great and I found a Test class for it, but it doesn't work.  

I've included the Class and the Test Class and associated errors for test class

Class:
global class UnlockRecordBatchJob implements Database.batchable<sObject> {
    //Start of this batch job
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id FROM Case LIMIT 50000000 '; //50 Million records
        return Database.getQueryLocator(query);
    }
    //Exeution of batch job
    global void execute(Database.BatchableContext BC, List<Case> scope) { //Scope max = 2000
        //List<Case> caseList = [SELECT Id From CaseLimit 2]; case already in scope varible
        //Check locked records
        List<Case> caseLockList = new List<Case>();
        for(Case c : scope)
        {
            if(Approval.isLocked(c.id)){
                caseLockList.add(c);
            }
        }
        
        if(!caseLockList.isEmpty()){
            //Unlock records
            List<Approval.UnlockResult> ulrList = Approval.unlock(caseLockList, false);
            
            // Iterate through each returned result
            for(Approval.UnlockResult  ulr : ulrList) {
                if (ulr.isSuccess()) {
                    //Operation was successful, so get the ID of the record that was processed
                    System.debug('Successfully locked account with ID: ' + ulr.getId());
                }
                else {
                    //Operation failed, so get all errors                
                    for(Database.Error err : ulr.getErrors()) {
                        System.debug('The following error has occurred.');                    
                        System.debug(err.getStatusCode() + ': ' + err.getMessage());
                        System.debug('Case fields that affected this error: ' + err.getFields());
                    }
                }
            }
        }
    }
    //finish job
    global void finish(Database.BatchableContext BC) {
    }
}
test Class:
@isTest
public class TestDemoTest {
    @isTest
    private static void testDemo(){
        List<Case> lcase=new List<Case>();
        for(integer i=0;i<10;i++){
            Case c=new Case();
            c.Status='New';
            c.Origin='Email';
            lcase.add(c);
            
        }
        insert lcase;
        List<Approval.LockResult> lrList = Approval.lock(lcase, false);
        Test.startTest();
        TestDemo td=new TestDemo();
        Database.executeBatch(td);
        Test.stopTest();
        
    }
}

The errors I get are:
Invalid type: TestDemo

Method does not exist or incorrect signature: void executeBatch(TestDemo) from the type Database



 
Best Answer chosen by Eric Blaxton 11
vishal-negandhivishal-negandhi
Hi Eric, 

If you have a look here - TRAILHEAD (https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_batch), in your test class, you need to invoke your actual batch class
Test.startTest();
        TestDemo td=new TestDemo();
        Database.executeBatch(td);
Test.stopTest();

SHOULD BE

Test.startTest();
        UnlockRecordBatchJob td=new UnlockRecordBatchJob();
        Database.executeBatch(td);
Test.stopTest();


This should help you fix your problem. 

Hope this helps.

All Answers

vishal-negandhivishal-negandhi
Hi Eric, 

If you have a look here - TRAILHEAD (https://trailhead.salesforce.com/en/content/learn/modules/asynchronous_apex/async_apex_batch), in your test class, you need to invoke your actual batch class
Test.startTest();
        TestDemo td=new TestDemo();
        Database.executeBatch(td);
Test.stopTest();

SHOULD BE

Test.startTest();
        UnlockRecordBatchJob td=new UnlockRecordBatchJob();
        Database.executeBatch(td);
Test.stopTest();


This should help you fix your problem. 

Hope this helps.

This was selected as the best answer
Eric Blaxton 11Eric Blaxton 11
Hello Vishal,

I tried your suggestion and I am getting this error.  

User-added image

Eric
vishal-negandhivishal-negandhi
Is your batch apex class already existing?
Eric Blaxton 11Eric Blaxton 11
Not sure what you mean. Yes it exists already. Do you mean, is there a duplicate? Get Outlook for iOS