2

I have two methods in a scheduled class. Both resemble the code below. I'm getting the following error when I call the second version.

System.AsyncException: The Apex job named "My batch Job" is already scheduled for execution.

I thought scheduled classes ran immediately when being tested? I can get around the problem by commenting out one version of "ScheduleMe"; it's only used for testing. I'd like to know if there's a way for me to successfully test both of the scheduling functions.

global static String scheduleMe() {
    AutoChatter SC = new AutoChatter(); 
    return System.schedule('Autochatter-Notification Checker', schedmidnight, SC);
}

Here's the test class:

@IsTest public class AutoChatter_test {
static testMethod void AutoChatter_test(){        

    Test.startTest();       
    AutoChatter.scheduleMe5ahead();        
    Test.stopTest();        
}

static testMethod void AutoChatter_test2(){                
    Test.startTest();
    AutoChatter.scheduleMe();        
    Test.stopTest();        
}

1 Answer 1

3

I've run into this before. I had written the scheduled class and had already run the ScheduleMe method before running my tests, so it refused to schedule again on top of the existing schedule. All I did was delete the scheduled job in my Sandbox before running the tests. You can still run both ScheduleMe methods in production once you've deployed the class.

Or maybe are both methods trying to schedule the job under the same name?

3
  • I'll have to check the name situation. I think they were different before but the same now. I'll have to check again to be absolutely sure.
    – Jagular
    Commented Mar 10, 2015 at 22:40
  • If the two ScheduleMe methods are trying to schedule their jobs under the same name, that may be the reason for the error.
    – OBerm
    Commented Mar 11, 2015 at 0:13
  • Having two different names made the difference. I also had a bug where a time with 55-59 in the minutes portion would result in an error.
    – Jagular
    Commented Mar 11, 2015 at 3:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .