Writing a Schedulable Batch Apex in Salesforce
- Sep
- 26
- Posted by Lin
- Posted in APEX, Salesforce
To use batch Apex, you have to create an Apex class that implements interface Database.Batchable in Salesforce, like the following sample code:
After this is done, you can monitor or manage the execution of it using the Salesforce user interface,
from Setup / Jobs / Scheduled Jobs
Here are some examples of schedule expression.
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!
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.


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. |
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!
Recent Posts
- Salesforce DevTools Release Notes (version 3.10.0) – mass edit custom fields on lightning
- Salesforce DevTools Release Notes (version 3.9.0) – Object Fields, SOQL Query Editor
- Salesforce DevTools Release Notes (version 3.8.0)
- Salesforce DevTools JUST HIT 10,000 USERS! THANK YOU!
- Using Promise for Apex Server-Side request in Lightning Component
Top Posts
- Salesforce DevTools – Powerful Salesforce developer tools, loved by over 50K developers - 228.7K views
- Date format and DateTime format - 211.2K views
- Salesforce Mass Editor (Chrome Extension) – Mass create, mass update, mass clone, mass delete on any list view. Support for both Classic and Lightning! - 161.8K views
- Writing a Schedulable Batch Apex in Salesforce - 126.5K views
- Setup an awesome Salesforce developer environment with Sublime Text 3 - 123.3K views
Categories
- APEX
- Apps
- Custom Label
- Custom Metadata
- Custom Setting
- Design & UI
- Event
- Heroku
- iOS & Object-c
- Lightnint Component
- Linux & Mac OS
- Metadata API
- Node.js
- Open Source
- Other
- PHP Framework
- Release Notes
- REST API
- Salesforce
- Salesforce DevTools
- Salesforce DX
- SOQL
- Tooling API
- Unit Tests
- Validation Rule
- Visualforce
- WordPress
You must be logged in to post a comment.