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
SF Beginner 2019SF Beginner 2019 

test class for unlocking record on batch

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) {
    }
}

I am stuck at 22% the Execute is not being covered
 
Best Answer chosen by SF Beginner 2019
Deepali KulshresthaDeepali Kulshrestha
Hi,


Try below code your code coverage is 93%.

To enable this feature, go to Setup | Search Process Automation Settings in the Quick Find box | click on  Process 

Automation Settings. Then, select Enable record locking and unlocking in Apex then save and used these codes.


@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();
        
    }
}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com

All Answers

Deepali KulshresthaDeepali Kulshrestha
Hi,


Try below code your code coverage is 93%.

To enable this feature, go to Setup | Search Process Automation Settings in the Quick Find box | click on  Process 

Automation Settings. Then, select Enable record locking and unlocking in Apex then save and used these codes.


@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();
        
    }
}



I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com
This was selected as the best answer
Eric Blaxton 11Eric Blaxton 11
Hi Deepali,

I cannot compile this test class.  I am getting an error on the line Database.executeBatch(td);
public class unlockOpTest {
   @isTest
    private static void testOp() 
    {        

List<Opportunity> listOp = new List<Opportunity>();
        
        Opportunity op1= new Opportunity();
          ----create op routine----       
          ---- add new ops to list----

 insert listOp;
        
        List<Approval.LockResult> lrList = Approval.lock(listOp, false);
        Test.startTest();
        unlockOpTest td = new unlockOpTest();
        Database.executeBatch(td);
        Test.stopTest();
			
    }
}

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

Thank you,
Eric