I spent hours troubleshooting my batch class but I cannot get it to update records. The ask is if a Opportunity closeDate >15th of the month, and CareStatus__c = 'ADM','AAMT' than Ready_toCommitToStart__c checkbox will be unchecked...There is also a custom metadata that is storing weekend dates that would need to be skipped if the 15th of the month is a weekend and move it to next business day.
My unit tests are passing which is what is confusing me...
Batch class:
global class CAREforceSSEStatusBatch implements Database.Batchable<sObject> {
public String query;
public Date fromDate = Date.today();
private Date nextMonth = fromDate.addMonths(1);
public Date firstOfNextMonth = nextMonth.toStartOfMonth();
private Set<String> caseListViewSet = new Set<String>{'ADMT', 'NEVR','AAMT'};
private Date endOfNextMonth = nextMonth.addMonths(1).toStartOfMonth();
private List<Date> observedDates;
// Constructor
/* global CAREforceSSEStatusBatch(String soql) {
query = soql; // Initializing inside the constructor
observedDates = getObservedDates();
}*/
// The batch job starts
global Database.QueryLocator start(Database.BatchableContext bc) {
String soqlQuery = 'SELECT Id, Name, Ready_to_CommitToStart__c, CloseDate, CareStatus__c FROM Opportunity WHERE Ready_to_CommitToStart__c = true AND CloseDate >=: firstOfNextMonth';
soqlQuery += ' AND CareStatus__c IN :caseListViewSet AND Is_SSE_Student__c = true AND RDS_Application_ID__c != NULL';
System.debug('>>>> start ' + soqlQuery);
return Database.getQueryLocator(soqlQuery);
}
global void execute(Database.BatchableContext bc, List<sObject> scope) {
observedDates = getObservedDates();
for (sObject record : scope) {
Opportunity opp = (Opportunity)record;
Date anticipatedStartDate = opp.CloseDate;
String careStatus = opp.CareStatus__c ;
// Check if the Anticipated Start Date is >15th of the month
if (anticipatedStartDate.day() > 15) {
// Uncheck the Commit to Start checkbox
opp.Ready_to_CommitToStart__c = false;
}
// Check if the Anticipated Start Date falls on an observed date
if (observedDates.contains(anticipatedStartDate)) {
// Extend the Commit to Start deadline until the following applicable business day
opp.CloseDate = getNextBusinessDay(anticipatedStartDate);
}
}
update scope; // Update the CARE profile records with the changes
}
global void finish(Database.BatchableContext bc) {
}
// Get the observed dates from custom metadata
private List<Date> getObservedDates() {
List<Date> dates = new List<Date>();
for (SSE_Status_Cutoff_Dates__mdt cutoffDate : [SELECT id, Cutoff_Date__c FROM SSE_Status_Cutoff_Dates__mdt]) {
dates.add(cutoffDate.Cutoff_Date__c);
}
return dates;
}
// Get the next business day after the provided date
private Date getNextBusinessDay(Date inputDate) {
Date nextBusinessDay = inputDate;
do {
nextBusinessDay = nextBusinessDay.addDays(1);
} while (nextBusinessDay.day() == 1 || nextBusinessDay.day() == 7 || observedDates.contains(nextBusinessDay));
return nextBusinessDay;
}
}
Test class:
@isTest
public class CAREforceSSEStatusBatchTest {
@TestSetup
static void setupData(){
// Arrange
Contact testContact = new Contact(FirstName = 'Test', LastName = 'Contact');
insert testContact;
Date closeDate = Date.today().addMonths(1).addDays(16);
Opportunity testOpportunity = new Opportunity(
Ready_to_CommitToStart__c = true,
Name = 'Test Name ASD15',
StudentContact__c = testContact.Id,
StageName = 'Prospect',
CloseDate = closeDate,
CareStatus__c = 'AAMT',
Is_SSE_Student__c = true,
RDS_Application_ID__c = '2314546515'
);
insert testOpportunity;
Date closeDate2 = Date.newInstance(2024, 4, 15);
Opportunity testOpportunity2 = new Opportunity(
Ready_to_CommitToStart__c = true,
Name = 'Test Name ASDMetadata',
StudentContact__c = testContact.Id,
StageName = 'Prospect',
CloseDate = closeDate2,
CareStatus__c = 'ADMT',
Is_SSE_Student__c = true,
RDS_Application_ID__c = '23145465156'
);
insert testOpportunity2;
}
@isTest
private static void testOpportunityASD15() {
Test.startTest();
String query = 'SELECT Id, Name, Ready_to_CommitToStart__c FROM Opportunity LIMIT 1';
CAREforceSSEStatusBatch batch = new CAREforceSSEStatusBatch();
Database.executeBatch(batch);
Test.stopTest();
// Assert - Add assertions if necessary
List<Opportunity> lstOpps = [SELECT Id, Ready_to_CommitToStart__c FROM Opportunity WHERE Name = 'Test Name ASD15'];
Assert.areNotEqual(false, lstOpps[0].Ready_to_CommitToStart__c, 'Commit to Start changed' );
}
@isTest
private static void testOpportunityASDMetadata() {
Date closeDate2 = Date.newInstance(2024, 4, 15);
Test.startTest();
String query = 'SELECT Id, Name, Ready_to_CommitToStart__c FROM Opportunity LIMIT 1';
CAREforceSSEStatusBatch batch = new CAREforceSSEStatusBatch();
Database.executeBatch(batch);
Test.stopTest();
// Assert - Add assertions if necessary
List<Opportunity> lstOpps = [SELECT Id, CloseDate FROM Opportunity WHERE Name = 'Test Name ASDMetadata'];
Assert.areNotEqual(closeDate2, lstOpps[0].CloseDate, 'The Closed Date changed' );
}
@isTest
private static void testScheduledBatchExecution() {
// Schedule the batch job
Test.StartTest();
CAREforceSSEStatusBatchSchedulable scheduledBatch = new CAREforceSSEStatusBatchSchedulable();
String scheduleExpression = '0 0 23 * * ?'; // Run daily at 11:00 PM
System.schedule('Test status Check', scheduleExpression, scheduledBatch);
Test.stopTest();
// Assert - Verify if the batch job is scheduled correctly
}
}
Batch job schedule:
global class CAREforceSSEStatusBatchSchedulable implements Schedulable {
// Execute at regular intervals
global void execute(SchedulableContext ctx){
CAREforceSSEStatusBatch batch = new CAREforceSSEStatusBatch();
Database.executebatch(batch, 200);
}
}
Execute Anonymous code to schedule job:
System.schedule('Run Test Job skip date mohamed', '0 35 22 * * ?', new CAREforceSSEStatusBatchSchedulable());