Batch class is used when there is a need to process large amount of data(millions of records) that would exceed normal processing limits. This is the asynchronous way to process the records
and is specifically designed to process bulk data or records together and have a greater governor limit than the synchronous code.
Why need Batch class ?
Say, I have 500 opportunities and each one of them has 50,000 opportunity lineitems. Now total records that needs to be processed will be (500 * 50,000) = 2,50,00,000 records. How will I process these records as salesforce will not allow you to break governor limits.
Governor limits :
1. Total number of records retrieved by SOQLqueries : 50,000.
2. Total number of SOQLqueries issued : 200
3. Total number of DML statements issued : 150.
4. Total number of DML statements issued : 150.
5. Total number of records processed as a result of DML statements : 10,000.
Solution : The only answer is batch apex. Except this there is no other way to process this data. It will process up to 50 million records either you insert them ,update or delete them.
Now as per our example 2,50,00,000
Records to be processed : 2,50,00,000
Batch size : 10,00,000
No of batches : 2,50,00,000/10,00,000 = 25
Implementing Batch Apex
To implement batch apex we must need to implement Database.Batchable interface to tell the salesforce that it is a batch class which will process millions of records.
It has the following three methods that need to be implemented −
Batch apex Syntax :
Start method is called automatically at the beginning of apex job and will be called only once. This method returns the Database.QueryLocator object that contains records to use in batch job or an iterable. You can retrieve up to 50 million records using QueryLocator.
Most of the time a QueryLocator does the trick with a simple SOQL query to generate the scope of objects in the batch job. But if you need to do something crazy like loop through the results of an API call or pre-process records before being passed to the execute method, you might want to check out the Custom Iterators link in the Resources section.
Actual processing begins here. It will pick records from queryLocator process them and then pick another records as per batch size. This process continues until all the records get processed. You can set the batch size as per your need, if not set it will set default size of 200.
The Database.executeBatch method takes two parameters :
A reference to the Database.BatchableContext object.
A list of sObjects
This method executes at last after all the methods finish their execution. It is used to execute post processing tasks ( for example send mail task).
One most important thing you can do in finish method is batch chaining. That I will explain in another blog.
Calling on Batch Apex
To make a call for batch class simply instantiate it and then call Database.executeBatch method:
BatchAccountUpdate obj = new BatchAccountUpdate ();
Id batchId = = obj.Database.executeBatch(obj);
You can also pass a scope parameter(optional) to process the number of records as per your need. This should be set based on logic if the logic is complex and you have set batch size to higher limit then off-course batch can get fail.
Up to five queued or active batch jobs are allowed for Apex.
A maximum of 50 million records can be returned in the Database.QueryLocator object. If more than 50 million records are returned, the batch job is immediately terminated and marked as Failed.
The start, execute, and finish methods can implement up to 10 callouts each.
The maximum number of batch executions is 250,000 per 24 hours.