function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
HareHare 

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
Vinita_SFDCVinita_SFDC
Hello,

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
Satyendra RawatSatyendra Rawat
Hi Vinita,
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
sfdc_ninjasfdc_ninja
Here is a very simple example of a Batch class.  This is not a very useful batch class as all it does is change the name of all Accounts to 'New Name'.  It does however show how a batch class is constructed and works to break large jobs into manageable chunks to get around governor limits.  In order to write a batch class you have to do 4 basic things
  1. Make sure your class implements the Batchable interface
  2. Implement the Start Method.  This is basically used to collect all the records you want to work on within the batch job.
  3. Implement the Execute Method.  this is where the logic is performed on the each chunk of records you gathered in the start method.
  4. Implement the Finish Method.  This is basically a method that runs after the batch job is complete and you can either send emails or do some post processing logic.  The finish method doesn't necessarily need to have actual processing going on in it.  I have done batch jobs in the past where the finish method is nothing more than a debug statement saying 'Job Complete'.


global class MyBatchClass implements Database.Batchable<sObject> {

global final string query;

global MyBatchClass() {
query = 'Select Id, Name From Account';
}

//Gather all the records I want to use int he execute method
global Database.QueryLocator start(Database.BatchableContext BC) {
 return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<sObject> scope) {
 
//The execute receives a list of sObjects as the scope for that 'chunk' of data.  So we have to cast the object to the
//appropriate object type
List<Account> accounts2Update = new List<Account>();
    
for(Sobject s : scope){
             Account a = (Account) s;
             a.Name = 'New Name';
             accounts2Update.add(a);     
             } 
             update accounts2Update;
}

global void finish(Database.BatchableContext BC) {
system.debug('Batch Job is Complete');
}

}


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{
public static String Sched = '0 00 00 * * ?';  //Every Day at Midnight
   
global static String scheduleMyJob() {
ScheduledMyBatchClass SMBC = new ScheduledMyBatchClass();
return System.schedule('My Scheduled Batch Job', Sched, SMBC);
}

global void execute(SchedulableContext sc) {
MyBatchClass myBatch = new MyBatchClass();
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.
HareHare
Thanks to all for respons and i will go through that
maylormaylor
@sfdc_ninja your tutorial here is great - thank you. I was able to successfully create a batch class and schedule class and run in a sandbox. The next hurdle is to figure out how to create a test class to promote the classes to production. Using your MyBatchClass example above, what would a test class look like? Thanks.