How to Schedule Batch Apex in Salesforce

on

|

views

and

comments

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

  1. The Name of the Job can be anything
  2. The Cron Expression
  3. The instance of Schedulable Class

Note: – The third parameters can be of any apex class.

Resources

Amit Singh
Amit Singhhttps://www.pantherschools.com/
Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion
Share this

Leave a review

Excellent

SUBSCRIBE-US

Book a 1:1 Call

Must-read

How to Utilize Salesforce CLI sf (v2)

The Salesforce CLI is not just a tool; it’s the cornerstone of development on the Salesforce Platform. It’s your go-to for building, testing, deploying, and more. As one of the most important development tools in our ecosystem

Save the day of a Developer with Apex Log Analyzer

Table of Contents What is Apex Log Analyzer? Apex Log Analyzer, a tool designed with Salesforce developers in mind, is here to simplify and accelerate your...

Salesforce PodCast

Introduction Hey Everyone, Welcome to my podcast, the first-ever podcast in India for Salesforce professionals. Achievement We are happy to announce that we have been selected as Top...

Recent articles

More like this

LEAVE A REPLY

Please enter your comment!
Please enter your name here

5/5

Stuck in coding limbo?

Our courses unlock your tech potential