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.
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.
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
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.
The execute method takes one parameter which is of type SchedulableContext and is used to monitor the Scheduled Jobs.
As we have talked about the interface and the execute method. Here is the Completed code for the scheduler 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.
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
Note: – The third parameters can be of any apex class.