Concepts or coding lessons of Salesforce that you can implement easily

Schedulable Batch Apex In 3 Easy steps In Salesforce

There are three main steps involved in Schedulable Batch Apex

1. Write a Batch Class
2. Write a Scheduled Apex class which execute the above Batch Class
3. Schedule the Scheduled Apex class from the Developer Console or from UI

So here we go...

Step 1. Write Batch Class

global class BatchApexClassExample implements Database.Batchable<SObject>{
 global Database.QueryLocator start(Database.BatchableContext BC) { 
  String soqlquery = 'SELECT Id, name, phone FROM Account';
  return database.getquerylocator(soqlquery);
 // The batch job executes and operates on one records batch of max 200
 global void execute(Database.BatchableContext BC, List<SObject> scope) { 
   // your logic
   System.debug('In BatchApexClassExample - Execute Method ');
 // The batch job finishes
 global void finish(Database.BatchableContext BC) { 
   System.debug('BatchApexClassExample Batch job completed successfully.');

Step 2. Write a Scheduled Apex which execute the above Batch Class

global with sharing class ScheduleBatchApexClassExample implements Schedulable {
 global void execute(SchedulableContext sc) {
  ID BatchId = Database.executeBatch(new BatchApexClassExample(), 200);

Step 3. Schedule the class from the Developer Console or from UI
  •  From UI : Go to Setup -> Apex Classes -> Click on Schedule Apex button
  •  From Developer Console, check below 
 Execute a schedulable Apex class with System.schedule method

 System.schedule('ScheduleBatchApexClassExampleScheduler', '0 0 * * * ?', new ScheduleBatchApexClassExample());

Good job, we are done. 

If you want to see your batch job scheduled then 

  • Go to Setup—>Monitor –> Scheduled Jobs

If you want to Schedule a Class in Every 5 Mins in Salesforce, since this is not possible to do through standard Salesforce User interface.

Yes I have solution for it. Use above small code snippets to do so for every 5 minutes.

Check my next post Run Schedule a Class In Every 5 Mins in Salesforce

Some important things about Schedulable Batch Apex :

Syntax for CRON expression:

Seconds Minutes Hours Day Month Week Year

Year is optional.

Seconds                         :- 0–59
Minutes                         :- 0–59
Hours                         :- 0–23
Day (Day_of_month) :- 1–31
Month                         :- 1–12
Week (Day_of_week)         :- 1–7

Year                             :- null or 1970–2099

The following are some examples of cron expressions:

0 0 13 * * ?Class runs every day at 1 PM.
0 0 22 ? * 6LClass runs the last Friday of every month at 10 PM.
0 0 10 ? * MON-FRIClass runs Monday through Friday at 10 AM.
0 0 20 * * ? 2010Class runs every day at 8 PM during the year 2010.
Apex Scheduler Limits:

  • You can only have maximum 100 scheduled Apex jobs at one time.
  • The maximum number of scheduled Apex executions per a day or 24-hour period is 250,000 or the number of user licenses in your organization multiplied by 200, whichever is greater in number.

Apex Scheduler Notes and Best Practices:

  • Salesforce schedules the apex class for execution at the specified time. Actual execution may be delayed based on salesforce service availability.
  • In Salesforce, Synchronous Web service callouts are not supported from scheduled Apex class. To be able to make callouts, make an asynchronous callout by placing @future(callout=true) annotation in class method and call this method from scheduled Apex class.
  • If any apex jobs scheduled to run during a Salesforce service maintenance downtime, those will be scheduled to run after the service comes online again, when system resources become available.
Enjoy! If you have any questions, comments, please feel free to let me know. As always, please feel free to get in touch me as I would be more than happy to assist you with any of your Salesforce development needs.

Next post: Run Schedule a Class In Every 5 Mins in Salesforce

1 comment:

  1. 1. how many ways to schedule batch apex
    2. in execute method how many records we can fetch
    3. difference between batch apex and schedule apex