How can i call a schedule class execute method from normal apex class or apex trigger. I also need to pass some variables like(daily,weekly,monthly) based on which i like to schedule the class like weekly or monthly or daily. Is it possible?
3 Answers
I think you can use something like following:
global class Dummy_Schedulable_Class implements Schedulable{
global String dailyCronExpression = '0 0 13 * * ?';
global String monthlyCronExpression = '0 0 13 1 * ?';
global String weeklyCronExpression = '0 0 13 ? * 1';
global void execute(SchedulableContext SC) {
}
public static void scheduleThis(String argPass){
String cronExp;
if ( 'daily'.equals( argPass ) ) {
cronExp = dailyCronExpression;
} else if ( 'monthly'.equals( argPass ) ) {
cronExp = monthlyCronExpression;
} else if ( 'weekly'.equals( argPass ) ) {
cronExp = weeklyCronExpression;
}
System.assertNotEquals(null, cronExp, 'Please pass "daily" "monthly" or "weekly" argument to this method.');
System.schedule('test', cronExp, new Dummy_Schedulable_Class());
}
}
and then call
Dummy_Schedulable_Class.scheduleThis('monthly');
or
Dummy_Schedulable_Class.scheduleThis('daily');
or
Dummy_Schedulable_Class.scheduleThis('weekly');
String constant dailyCronExpression = '0 0 13 * * ?' means 'every day at 1 p.m.' String constant dailyCronExpression = '0 0 13 1 * ?' means 'every first day of month at 1 p.m.' String constant dailyCronExpression = '0 0 13 * * ?' means 'every first day of week at 1 p.m.'
Please mark this answer as accepted if you agree it accurately and precisely answers your question, please vote up this answer if you find it useful.
You can just create an instance of the class and then call the method directly, using null
for the context:
MySchedulableClass m = new MySchedulableClass();
m.execute(null);
As you can see though, that doesn't have any extra parameters and you can't add them since it's a defined interface. What you should do is put all the code that does the work elsewhere in it's own class, and then call that from your 'normal' code and also from the schedulable classes execute()
method.
New class
public class WorkerClass
{
public WorkerClass() {}
public void DoWork(String s1, String s2, String s3)
{
// code goes here
}
}
Schedulable Class
global class MySchedulableClass implements Schedulable
{
global static void scheduleMe(String mode)
{
MySchedulableClass msc = new MySchedulableClass();
String sch = '20 30 8 10 2 ?'; // base this on mode or whatever your params are
String jobID = system.schedule('Scheduled Job', sch, msc);
}
global void execute(SchedulableContext sc)
{
WorkerClass w = new WorkerClass();
w.DoWork('Hello', 'World', '!');
}
}
To schedule the class from elsewhere
Just call the static method:
MySchedulableClass.scheduleMe('Daily');
To just use the main worker elsewhere
Just take the content of execute()
and use it wherever you need to run that code in a synchronous manner:
WorkerClass w = new WorkerClass();
w.DoWork('Hello', 'World', '!');
-
1Good answer dude, maybe worth adding some thoughts on bulkifiation if doing this kind of thing in Apex Trigger, the developer guide has a few warnings about this you can probably call out. Though personally I tend to avoid using batch apex from a trigger all together if i can, but thats just me. :-) Dec 19, 2013 at 13:05
all you need is to use schedule apex classes provided by salesforce ..implement your apex class with schedulable interface and u can schedule your apex class as monthly or daily ..for more reference go through this link
Happy Coding !!