Hello friends, In this post, you will see a code example that helps you to scheduled a batch class without any scheduler class. With the help of this post you can directly schedule any batch class. so let's get started,
Schedule batch class without scheduler class
To scheduled a batch class, we have to create a scheduler class that call tha batch apex like below code snippet.
DemoScheduler.apxc
global class DemoScheduler implements Schedulable{
global void execute(SchedulableContext SC){
Database.executebatch(new DemoBatchClass());
}
}
But now no need of this extra scheduler class as written in above sample code. To schedule batch class directly we have to implement Schedulable interface inside batch class like below example.
MyBatchClass.apxc
global class MyBatchClass implements Database.Batchable<sObject>,Schedulable{
global Database.QueryLocator start(Database.BatchableContext bc){
//query here...
return Database.getQueryLocator('SELECT Id FROM Account');
}
global void execute(Database.BatchableContext bc,List<Sobject> scope){
//execute logic on queried data...
}
global void finish(Database.BatchableContext BC){
//post execution logic...
}
global void execute(SchedulableContext sc) {
Database.executeBatch(new MyBatchClass(),70);
}
}
Output:
Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
Thank you.
0 Comments
Post a Comment