You need to sign in to do that
Don't have an account?
snakeroot
Testing Multiple Scheduled Apex Jobs
I'm getting the following error intermittently when running a scheduled batch class: Attempted to schedule too many concurrent batch jobs in this org (limit is 5).
I found out why I'm gettting this error and the code to use to maneuver around this limitation in this KB article (https://help.salesforce.com/apex/HTViewSolution?id=000182449" target="_blank). I'm able to test for the code in the if part of the if/else statement to get code coverage, but I can't figure out how to test the else part of the statement. If anyone could help me out with this, I'd appreciate it. Here's the code and the test coverage code:
Schedulable Class:
Test Code:
I found out why I'm gettting this error and the code to use to maneuver around this limitation in this KB article (https://help.salesforce.com/apex/HTViewSolution?id=000182449" target="_blank). I'm able to test for the code in the if part of the if/else statement to get code coverage, but I can't figure out how to test the else part of the statement. If anyone could help me out with this, I'd appreciate it. Here's the code and the test coverage code:
Schedulable Class:
global void execute(SchedulableContext sc) { try{ //check if there are 5 active batch jobs //In some cases, might need to add "Status='Queued' " to the criteria if ([SELECT count() FROM AsyncApexJob WHERE JobType='BatchApex' AND (Status = 'Processing' OR Status = 'Preparing')] < 5){ CatchOfTheDayBatch controller = new CatchOfTheDayBatch('SELECT Id,Name,Retention_Target_Name_LOST__c,Retention_Target_ID_LOST__c, Retention_Target_Email_Date_LOST__c,Authorization_Signature__r.Id FROM Account WHERE RecordType.Name = \'RR Companies\' AND Retention_Target_ID_LOST__c != null ORDER BY Retention_Target_ID_LOST__c DESC'); Integer batchSize = 10; database.executebatch(controller , batchSize); } else { //schedule this same schedulable class again in 30 mins CatchOfTheDay scheduledClass = new CatchOfTheDay(); Datetime dt = Datetime.now() + (0.024305); // i.e. 30 mins String timeForScheduler = dt.format('s m H d M \'?\' yyyy'); Id schedId = System.Schedule('MatrixRetry'+timeForScheduler,timeForScheduler,scheduledCLass); } } catch (System.EmailException e) { System.debug('An error has occurred: '+e); } } }
Test Code:
@isTest private class Test_CatchOfTheDay { public static String CRON_EXP = '0 0 0 15 3 ? 2022'; static testmethod void test() { Test.startTest(); String jobId = System.schedule('CatchOfTheDay', CRON_EXP, new CatchOfTheDay()); CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE id = :jobId]; System.assertEquals(CRON_EXP, ct.CronExpression); System.assertEquals(0, ct.TimesTriggered); System.assertEquals('2022-03-15 00:00:00', String.valueOf(ct.NextFireTime)); Test.stopTest(); } }
Scheduleable class:
Test class: