You need to sign in to do that
Don't have an account?
SF Beginner 2019
I am stuck at 22% the Execute is not being covered
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
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
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
I cannot compile this test class. I am getting an error on the line Database.executeBatch(td);
Error message:
Thank you,
Eric