Table of Contents
Introduction
In the previous blog post, we talked about what is Batch Apex in Salesforce and we also have discussed about that we can schedule the batch apex to run at a particular interval.
Here, in this post we will see how we can schedule a batch apex or even a simple apex class.
Implement interface
As we wanted to schedule a batch class or any simple class we need to implement an interface which is provided by System Namespace by Salesforce.
The name of the Interface is Schedulable & as this interface is inside System namespace the full name would be System.Schedulable
public class BatchScheduleUpdate implements System.Schedulable{}
Implement Schedulable Interface Method
System.Schedulable interface has on method which we need to implement inside our apex class and this is the method which will execute every interval our class is scheduled for.
From execute method we do either call our Batch Apex or Our Simple Apex Class Method.
public void execute(SchedulableContext SC) {
UpdateAccountFields batch = new UpdateAccountFields()
ID batchprocessid = Database.executeBatch(batch);
/* Query the AsyncApexJob to get the batch Status */
AsyncApexJob aaj = [SELECT Id, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors
FROM AsyncApexJob WHERE ID =: batchprocessid ];
}
The execute method takes one parameter which is of type SchedulableContext and is used to monitor the Scheduled Jobs.
Scheduler Class
As we have talked about the interface and the execute method. Here is the Completed code for the scheduler class.
public class BatchScheduleUpdate implements System.Schedulable{
public void execute(SchedulableContext SC) {
UpdateAccountFields batch = new UpdateAccountFields()
ID batchprocessid = Database.executeBatch(batch);
/* Query the AsyncApexJob to get the batch Status */
AsyncApexJob aaj = [SELECT Id, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors
FROM AsyncApexJob WHERE ID =: batchprocessid ];
}
}
Schedule the Class
Now, as we have developed the Apex Class we need to scheduled it to run at certain interval.
In this example we will schedule the class to run at every 1 hour.
First We need to prepare the CRON Expression so that we can tell the Salesforce system to execute the class at every 1 hour.
You can generate the CRON Expression for your need from here.
Execute the below code from Developer Console.
BatchScheduleUpdate schedulableClass = new BatchScheduleUpdate();
String CRON_EXP = '0 0 * * * ?';
String jobID = System.schedule('Schedule Class Hourly', CRON_EXP, schedulableClass);
Explanation
In the above code the first line is nothing it is the Object of the Schedulable Class.
The Second line is the Cron Expression which tells Salesforce that you need to execute the Schedulable Class execute method at every 1 hour interval.
The third line is scheduling the class which accepts 3 parameters
- The Name of the Job can be anything
- The Cron Expression
- The instance of Schedulable Class
Note: – The third parameters can be of any apex class.