1
votes

I have many classes that implement Schedulable Interface. I want to schdule multiple Apex classes at a time through an another apex class. So I need to query all the classes that are implement Schedulable interface.

I am using the following code snipet to achieve this, but I am getting a compiler error like below

ERROR: "You must select an Apex class that implements the Schedulable interface. at line 127 column 13"

CODE:

list<ApexClass> listClasses; 

String input='0 0 8 13 2 ?';

listClasses=[Select Id, Name,body from ApexClass]   
for(ApexClass a:listClasses){            
        system.schedule(a.Name+' AutoScheduler', input, a);    
}

Question: How do I query all the apex class which implement schedulable interface? So that I can directly pass it to system.schedule() method.

DIFFERENT TRY: When after getting this error I tried to query only one apex class(Known Class) which implements schedulable interface. Again no use. Please see the below snipet for the different try

CODE:

list<ApexClass> listClasses; 

String input='0 0 8 13 2 ?';

//Retriving only one class of Name='SchedularTest' 

listClasses=[Select Id, Name,body from ApexClass where Name ='SchedularTest']   
for(ApexClass a:listClasses){            
        system.schedule(a.Name+' AutoScheduler', input, a);    
}

ERROR: "You must select an Apex class that implements the Schedulable interface. at line 127 column 13"

Thanks in advance

Satheeskumar

1
Did you know we have a stackexchange site dedicated to salesforce. Feel free to check that out salesforce.stackexchange.comPrady

1 Answers

5
votes

I'd say the error message is quite clear?

The last parameter you're passing to System.schedule has to be a new object. New instance of the class. What you're passing is the metadata information about the class, it's body etc... This stuff can't be "run" like that.

I'm going to bet that system.schedule('SchedulerTest AutoScheduler', input, new SchedulerTest()); will work OK? If it doesn't - make sure the class compiles OK and maybe also whether it is marked as valid.

See? how do you expect something that represents a row in the database / file on disk to "work" like an object of the class?

If you need to create a new object of given class having only this class' name - you might want to check Type methods.

String className = 'SchedulerTest';
Type t = Type.forName(className);

System.schedule(className, '0 0 8 13 2 ?', (Schedulable)t.newInstance());

This makes a new object (it's actually a generic Object) and then you cast it to something that's acceptable for the method. You'll get a runtime failure if the class doesn't implement the interface:

System.TypeException: Invalid conversion from runtime type SchedulerTest to system.Schedulable