Salesforce Asynchronous Apex: How to Batchable Apex Class from Schedulable Class

 

Looking for Salesforce Training & HandsOn Projects?

Trailblazer Profile | LinkedIn | Salesforce Blog | Facebook | Youtube Channel | WhatsApp Community

Batchable and Schedulable classes can be a powerful tool for automating tasks in Salesforce. 

It is possible to call a Batchable class from a Schedulable class. This design pattern can help us to schedule the Batch Job & run them at specific intervals. 

In this design pattern, Schedulable Class will create a new instance of the Batchable class & then enqueues it. The Batchable class will then be executed by the Salesforce platform. 

You can explore the details of Batchable & Schedulable classes in a few of my earlier articles:

 Batchable Apex

Schedulable Apex

In this article, we will explore the implementation details of this design pattern. 

We can start with a simple demo to explain the concept in the following steps: 

Step-1 to Step-7: In these steps, we will be creating a brand new Batchable Class & get it ready to be scheduled by a Schedulable Class later. If need a detailed explanation of Batchable classes please refer to the links provided earlier in this article.





Step-8: We will be adding a brand new Apex class & implement the Schedulable interface to make it a Schedulable Class

 

Step-9: We will execute the Batch class from the “execute” method of the Schedulable Class


Step-10: Then we will Schedule the Job using the Cron expression using the “System.schedule” method 

Step-11: Notice how we are passing the instance of the Schedulable class


Step-12 to Step-16: We can view & monitor the Scheduled Jobs



Step-17: Shows how the records have been created by Batch Class which was triggered by the Scheduled Job


Hope you enjoyed this article. Please leave your comments to let me know how you like the content and how you find it helpful to learn the topic.


Trailblazer Profile | LinkedIn | Salesforce Blog | Facebook | Youtube Channel | WhatsApp Community




Comments