You need to sign in to do that
Don't have an account?
sg
Test class coverage for a batch class which includes business hours
I have this batch class which included Busines hours check boolean method. I am having hard time including business hours check in test records and can't find a correct reference too. Can someone help me with a piece of code to include Business hours check logic in Test class?
Below is the batch class
global class BatchCounter implements Database.Batchable<sObject>
{
static final String businessHoursId;
global Database.QueryLocator start(Database.BatchableContext bc)
{
String query = 'SELECT Id, numberField__c,statusField__c,Submitteddate__c FROM objectName__c WHERE Submitteddate__c !=null '+' AND statusField__c IN (\'Assigned\', \'Submitted\', \'InProgress\')'; return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<TMA__c> tmas)
{
for (TMA__c tma : tmas)
{
if (isBusinessHours(businessHoursId))
{
tma.numberField__c+=1;
}
}
update tmas;
}
global void finish(Database.BatchableContext bc)
{ }
private static Boolean isBusinessHours(Id businessHoursId)
{
BusinessHours businessHours = [SELECT Id FROM BusinessHours WHERE Id = :businessHoursId];
if (businessHours != null)
{
Datetime currentDatetime = Datetime.now();
return BusinessHours.isWithin(businessHoursId, currentDatetime);
}
return false;
} }
Below is the batch class
global class BatchCounter implements Database.Batchable<sObject>
{
static final String businessHoursId;
global Database.QueryLocator start(Database.BatchableContext bc)
{
String query = 'SELECT Id, numberField__c,statusField__c,Submitteddate__c FROM objectName__c WHERE Submitteddate__c !=null '+' AND statusField__c IN (\'Assigned\', \'Submitted\', \'InProgress\')'; return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc, List<TMA__c> tmas)
{
for (TMA__c tma : tmas)
{
if (isBusinessHours(businessHoursId))
{
tma.numberField__c+=1;
}
}
update tmas;
}
global void finish(Database.BatchableContext bc)
{ }
private static Boolean isBusinessHours(Id businessHoursId)
{
BusinessHours businessHours = [SELECT Id FROM BusinessHours WHERE Id = :businessHoursId];
if (businessHours != null)
{
Datetime currentDatetime = Datetime.now();
return BusinessHours.isWithin(businessHoursId, currentDatetime);
}
return false;
} }
To include the business hours check logic in your test class for the BatchCounter batch class, you can create test records with different scenarios and set the Submitteddate__c field accordingly. Here's an example of how you can modify your test class to cover the business hours check:
If it helps , please mark this as Best Answer.
Thank you.