You need to sign in to do that
Don't have an account?

Hi am new to salesforce can any one please explane me how to use batch apex with simple examples
Hi am new to salesforce can any one please explane me how to use batch apex with simple examples
function readOnly(count){ }
You need to sign in to do that
Don't have an account?
A developer can now employ batch Apex to build complex, long-running processes on the Force.com platform.
For example, a developer could build an archiving solution that runs on a nightly basis, looking for records past a certain date and adding them to an archive. Or a developer could build a data cleansing operation that goes through all Accounts and Opportunities on a nightly basis and updates them if necessary, based on custom criteria. Batch Apex is exposed as an interface that must be implemented by the developer.
Batch jobs can be programmatically invoked at runtime using Apex. You can only have five queued or active batch jobs at one time. You can evaluate your current count by viewing the Scheduled Jobs page in Salesforce or programmatically using SOAP API to query the AsyncapexJob object.
Please refer following links for further details:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch.htm
Salesforce provide a Database.Batchable interface which is having three method start(...), execute(....) and finish(...) method,
If you want use the batch apex then simple implement the Database.Batchable in your class
and implements the all three method in your class.
if you want to schedule this batch write other class and implements the Schedulable interface it have only one method execute(..)
If you need further information, feel free ask me.
Thanks
Satya
global class MyBatchClass implements Database.Batchable<sObject> {
global final string query;
global MyBatchClass() {
//Gather all the records I want to use int he execute method
global Database.QueryLocator start(Database.BatchableContext BC) {
global void execute(Database.BatchableContext BC, List<sObject> scope) {
//appropriate object type
List<Account> accounts2Update = new List<Account>();
for(Sobject s : scope){
a.Name = 'New Name';
accounts2Update.add(a);
update accounts2Update;
}
global void finish(Database.BatchableContext BC) {
}
Now you have your Batch Apex. In order to run this, all you would need to do would be go to an execute anonymous window and use this line of code. The 50 parameter is what is setting the size of each batch for the execute method to be passed each time.
MyBatchClass myBatch = new MyBatchClass();
ID batchprocessid = Database.executeBatch(myBatch,50);
If you wanted to schedule this job, that is fairly easy as well with Scheduled apex. You could write another Apex class that implements the Schedulable interface and run this batch Apex at regular intervals. The example below would be used to schedule this job to be run each night at midnight.
global class ScheduledMyBatchClass implements Schedulable{
global static String scheduleMyJob() {
return System.schedule('My Scheduled Batch Job', Sched, SMBC);
global void execute(SchedulableContext sc) {
ID batchprocessid = Database.executeBatch(myBatch,50);
Now you just have to go to an execute anonymous window and use the line
ScheduledMyBatchClass.scheduleMyJob();
You now have your new batch class scheduled to run every day at midnight.
I hope this helps.