Writing a Schedulable Batch Apex in Salesforce

To use batch Apex, you have to create an Apex class that implements interface Database.Batchable in Salesforce, like the following sample code:

Execute batch

Then you can use Database.executebatch method to execute it.

Schedule Apex job

To schedule your batch Apex to run at regular intervals, you also need an Apex class that implements interface Schedulable, like the following sample code:

Schedule Apex via user interface

Click “Schedule Apex” button in Setup / Develop / Apex Classes to create a schedule apex job.
apex-classes-salesforce-developer-edition schedule-apex-user-interface After this is done, you can monitor or manage the execution of it using the Salesforce user interface, from Setup / Jobs / Scheduled Jobs

Schedule Apex job via Apex code

Or you can execute a schedulable Apex class with System.schedule method.

Here are some examples of schedule expression.

Expression Description
0 0 13 * * ? Class runs every day at 1 PM.
0 0 22 ? * 6L Class runs the last Friday of every month at 10 PM.
0 0 10 ? * MON-FRI Class runs Monday through Friday at 10 AM.
0 0 20 * * ? 2010 Class runs every day at 8 PM during the year 2010.
More informations you can reference HERE

So far, we have created a schedulable batch apex that implements Database.Batchable<sObject> in Salesforce.
But this kind of batch Apex only supports sObject queries and results in start execution, and if you want to use SOQL having aggregate functions like SUM(), MAX(), COUNT(), you can solve it by following this post Using Aggregate SOQL queries/results in Batch Apex !

Enjoy it!