I have 20 scheduled APEX classes, and every time I want to change one or deploy new APEX I get an error for every Scheduled APEX test class like this:
Failure Message: "System.AsyncException: The Apex job named "ClientCare_AfterAction_MonSch" is already scheduled for execution.", Failure Stack Trace: "Class.ClientCare_AfterAction_MonSch.scheduleNow: line 36, column 1 Class.ClientCare_AfterAction_MonSch.execute: line 16, column 1"
So I have to delete every scheduled job, deploy the code, then reschedule. It's rather annoying.
I've asked this question before, and received a working answer (to change the name of the Scheduled job in the test class), but have since had to change the Schedulable class which has made the problem come up again and the previous solution no longer works.
Scheduable Class:
global class ClientCare_FundUpdate_MonthlySch implements Schedulable{
global void execute(SchedulableContext SC) {
ClientCare_FundUpdate_MonthlyEmail CCBatch = new ClientCare_FundUpdate_MonthlyEmail();
database.executebatch(CCBatch,100);
// this section of code will abort the current schedule job
try {
system.abortJob(sc.getTriggerId());
} catch (exception e) {system.debug('ClientCare_FundUpdate_MonthlySch schedule job exception while aborting:' + e);}
// reschedule the job
system.debug('ClientCare_FundUpdate_MonthlySch schedule job executing');
scheduleNow();
}
global static void scheduleNow() {
// this section of code will schedule the next execution 1 minute from now
datetime thisTime = system.now().addHours(24);
integer minute = thisTime.minute();
integer second = thisTime.second();
integer hour = thisTime.hour();
integer year = thisTime.year();
integer month = thisTime.month();
integer day = thisTime.day();
String timeStamp = second + ' ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ? ' + year;
string jobName = 'ClientCare_FundUpdate_MonthlySch';
ClientCare_FundUpdate_MonthlySch p = new ClientCare_FundUpdate_MonthlySch();
system.schedule(jobName, timeStamp , p);
/**
ClientCare_FundUpdate_MonthlySch m = new ClientCare_FundUpdate_MonthlySch();
String sch = '0 0 5 1 * ? * ' ;
system.schedule('ClientCare_FundUpdate_MonthlySch', sch, m);
*/
}
}
Test Class:
public static testmethod void DailySchEmailJobtest() {
Test.startTest();
String sch = '0 0 5 ? * * * ' ;
String jobId = System.schedule('ClientCare_FundUpdate_MonthlySch_Test',sch, new ClientCare_FundUpdate_MonthlySch());
Test.stopTest();
}