As I was on the look out for a solution I found that classes can be scheduled via apex and I tried writing a script to schedule my classes and to my luck It worked as i expected.As I thought there may be at least few of you who has faced the same issue I thought of sharing my code. Please note that this code may not be directly serve your purpose and will need some tweaking. But it will give you the basic idea of how classes can be scheduled via apex.
list<ApexClass> listClasses;
//create a string list and add the Names of classes you want to schedule
//please note that all of these classes must implement schedulable Interface
List<String> ClassesToBeScheduled = new List<String>(); ClassesToBeScheduled.add('FirstSchedule'); ClassesToBeScheduled.add('SecondSchedule'); ClassesToBeScheduled.add('ThirdSchedule'); ClassesToBeScheduled.add('FouthSchedule');
//query the salesforce apexclass object to get the records of classes you want to //schedule
for(ApexClass ac:[Select Id Name From ApexClass where Name IN:ClassesToBeScheduled])
{
String className = ac.Name;
String sch ='0 0 4 ? * * 2014'; // expersion specify how class is scheduled
//here the class is scheudled to run at 4.00 AM every day till the end of 2014
// you can learn all about schedule expressions here
Type t = Type.forName(className);
//schedule the class by passing
//1.jobName - any string (here I use class name)
//2.schedule frequncy - string experssion specify when and hoe often each class //is run
//3.Instance of the class to be scheduled
System.schedule(className, sch, (Schedulable)t.newInstance());
}
You can then run your script using developer console.
No comments:
Post a Comment