Chat with us, powered by LiveChat

Blog

How To Call Batch Apex By Scheduler Class Within Salesforce

call-batch-apex-by-scheduler-class-within-salesforceAnother question has been asked to our expert Salesforce development team and we thought it will be useful for other Salesforce users. The question is how to create an Apex scheduler class which will call the Apex Batch class automatically.
Here we will schedule a batch Apex using a declarative way and call the Apex scheduler class every 1 Hour. With this, we can schedule the class either weekly or monthly as an example.
In the scheduler class we can specify if it runs every day or any selected day of the month. You can schedule this class for a particular day and any specified time at which the class can be run. Using this declarative way, the highest frequency we can have is once per day within Salesforce and not more than that. For example, you want the class to run every 1 hour, then you cannot do it using this declarative way. If you want to do this, we can use system.schedule method and call the scheduler class accordingly.
With the system.schedule we pass parameters:
system.schedule (Name_of_job, Time_for_scheduling_job, Schedular_class_name)

 

Name_of_job: Give any name for this Job

 

Time_for_scheduling_job: Write the time interval for which it needs to run

 

Schedular_class_name: Name of your scheduler class

 

The Expression for the time interval should be specified in below format:
(Seconds  Minutes  Hours Day_of_month  Month  Day_of_week  optional_year)

 

Let’s use a real example now within Salesforce. We are going to write a simple batch apex class, its scheduler class and then, execute system.schedulemethod to run that batch apex every 1 hour.
Step 1:

 

Go To App Setup Develop Apex Classes

 

Click the “New” button to create new batch Apex class

 

image 1

 

Step 2:  Paste this code in Class editor

 

global class RunBatchApex implements database.batchable<sobject>{Public string queryString;Public void setQry(string queryString){this.queryString = ‘Select  id, name from account limit 1’;}global database.querylocator start(database.batchableContext bc){return database.getquerylocator(queryString);}global void execute(database.batchablecontext bd, list<sobject> sc){ }Public void finish(database.batchableContext bc){ }}

 

Save this code

 

Step 3: 

 

Go Again To App Setup → Develop → Apex Classes

 

Click the “New” button to create new Apex scheduler class

 

Paste this code in class editor

 

global with sharing class SchedularForBatchApex implements Schedulable {global void execute(SchedulableContext sc) {ID BatchId = Database.executeBatch(new RunBatchApex (), 200);} Public static void SchedulerMethod() {string con_exp= ‘0 0 1 * * ?’;System.schedule(‘RunBatchApexTest’, con_exp, new SchedularForBatchApex());}}

 

Step 4:

 

For executing this Apex scheduler class from the Salesforce developer console, open the developer console

 

image 2

image 3

 

Step 5:

 

Paste this code in the Salesforce developer console

SchedularForBatchApexSFBA = new SchedularForBatchApex ();String sch = ‘0 0 24 31 12 ?’;system.schedule(‘Execute Schedular’, sch, BWE);

 

This class is scheduled to run at 12 AM, on the 31st of Dec. We hope this example of how to call a batch Apex apex class by Apex scheduler class is helpful for you. As always if you have any questions or need any help with your Salesforce development, please feel free to reach out to the team at Mind Digital Group.

Tags: , ,

Calander Dec 19, 2013

Leave a Reply

Your email address will not be published. Required fields are marked *

Enter the text from the image below *