function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Bhavneesh SalujaBhavneesh Saluja 

Scheduling normal Apex Class

Can I schedule a normal apex class which has few batch classes executed within it?  For Ex: 
 public class A{
//Some Code....
    BatchCLass B= new BatchCLass();
  Database.executebatch(B,200);
Batchcls C= new Batchcls();
Database.executebatch(C,200);
//Some Code...
}
Now i want to write a scheduler class for Apex class A. Will normal Scheduler class work for this scenario  or is there any restriction from Salesforce on this.
Any leads would be appreciated. 

Thanks in Advance !!!
Best Answer chosen by Bhavneesh Saluja
Martijn SchwarzerMartijn Schwarzer
Hi Bhavneesh,

You will need to implement the scheduler interface in the class, like this:
 
global class A implements Schedulable {
    global void execute(SchedulableContext SC) {
        //Some Code....
        BatchCLass B= new BatchCLass();
        Database.executebatch(B,200);
        Batchcls C= new Batchcls();
        Database.executebatch(C,200);
        //Some Code...
    }
}
You can then use anonymous Apex to schedule the class like this:
 
A a = new A();
String CRON_EXP = '20 30 8 10 2 ?';
String jobID = system.schedule('Merge Job', CRON_EXP, a);

Hope this helps!

Best regards,
Martijn Schwärzer
 

All Answers

Martijn SchwarzerMartijn Schwarzer
Hi Bhavneesh,

You will need to implement the scheduler interface in the class, like this:
 
global class A implements Schedulable {
    global void execute(SchedulableContext SC) {
        //Some Code....
        BatchCLass B= new BatchCLass();
        Database.executebatch(B,200);
        Batchcls C= new Batchcls();
        Database.executebatch(C,200);
        //Some Code...
    }
}
You can then use anonymous Apex to schedule the class like this:
 
A a = new A();
String CRON_EXP = '20 30 8 10 2 ?';
String jobID = system.schedule('Merge Job', CRON_EXP, a);

Hope this helps!

Best regards,
Martijn Schwärzer
 
This was selected as the best answer
Bhavneesh SalujaBhavneesh Saluja
Hi Martijn , 

Thanks for the reply. However, I was thinking of doing something like this: 
 
public with sharing class A{
  //Some Code...
  public void myMethod(){
        BatchCLass B= new BatchCLass();
        Database.executebatch(B,200);
        Batchcls C= new Batchcls();
         Database.executebatch(C,200);
           //Some Code...
      }
}
Let this class be a standalone piece. And write a scheduler for this: 
 
global class Scheduler_A implements Schedulable{
   global void execute(SchedulableContext SC){
        A myClass = new A();
        myClass.myMethod();
  }
}

Please let me know if this would be a feasible solution to my problem stated, since i want Class A to be a standalone piece of code which i can execute from Trigger as well as scheduler class based on certain critera. 

Regards
Bhavneesh    
 
Martijn SchwarzerMartijn Schwarzer
Hi Bhavneesh,

If you want Class A to be a standalone piece, then your solution is perfect!

Happy coding!

Best regards,
Martijn Schwärzer

Ps. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.