To commence with, I will discuss what is the term Schedulable Apex. It means that you can schedule or execute an block of code at a particular time. You can either execute a class, a batch class, or a queueable class. In this blog I will elaborate all these methods to explain schedulable apex.
The Apex Scheduler lets you delay execution so that you can run Apex classes at a specified time. To schedule jobs, write an Apex class that implements the Schedulable interface, and then schedule it for execution on a specific schedule.
Why : Minimizing the lot of manual effort in daily routine.
1. To update daily records
2. To run batch class daily or at a particular time.
3. To send an email at a given time.
4. Daily or weekly maintenance tasks
1. From User interface
2. Programatically by system.schedule method
1. Schedulable Apex
To schedule any class or code you need to follow two steps :
1. First implement the Schedulable interface for the class
2. Schedule an instance of the class to run at a specific time using the System.schedule method.
Schedule a method
After you implement a class with the Schedulable interface, use the System.Schedule method to execute it. The System.Schedule method uses the user's timezone for the basis of all schedules.
2. Schedule a batch class
1. Create a batch class
2. Write a schedulable class and call batch class from it.
3. Schedule it.
Creating a batch class
Write a schedulable class and call batch class from it
To write a schedulable class you need to implement schedulable interface and implement the execute method as well. So put your logic into execute method.
Schedule the class
Now, write a cron expression and call the schedule method. Open the anonymous window and copy the below code:-
3. Schedule a Queueable Class
This is same as scheduling a batch class the difference is in this case you need to implement the queueable interface.
Schedulable class for above class
Now, As above mentioned write a cron expression and call the schedule method. Open the anonymous window and copy the below code:-