How To Call Batch Apex By Scheduler Class Within Salesforce | Techila
single,single-post,postid-2706,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive

How To Call Batch Apex By Scheduler Class Within Salesforce


How To Call Batch Apex By Scheduler Class Within Salesforce

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 withinSalesforce 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

New batch Apex class


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



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.

“Know more about Salesforce Consulting at Techila”

Author: techila

No Comments

Post A Comment