Wednesday 3 June 2015

Batch Apex in Depth

Basics of batch apex :-

Apex class that implements the Database.Batchable Interface is known as batch class .
We are basically using batch apex to avoid some governor limits .
There are two interface in  Database Namespace .
1.Batchable
Below are methods in Batchable interface
Public System.Iterable start(Database.BatchabelContext bcon)
Public Database.QueryLocator start(Database.BatchabelContext bcon)
Public void execute(Database.BatchableContext bc,List<Sobject> scope)
Public void finish(Database.BatchableContext bc)
2.BatchableContext
Below are the methods in the interface
 Public Id getJobId()
 public Id getChildJobId()
Basic thing is that once you will implement Batchable interface in your class you have to implement three methods in that interface .

Flow is that when you execute your class start method will execute once and return a list in two different form as we have two types of return type in start method .
The list which will return from the start method will available in execute methods scope execute method will execute according to your batch size .
Assume that you have 1000 record ,if you set your batch size as 100 then your execute method will execute 10 times .
Finally the finish method will execute once the execute method will complete the execution .
Basically in the signature of all method one common parameter we are passing named BatchabelContext, this is to get the batch  JobId and childJobId .
Both method returns the id of  "AsyncApexJob" object record , where  both have self relationship and child record will have different type and contain the last executed record id .

How to write simple  batch class :-

Below example will help you to update all account name with post fix as UpdatedbatchApex .
Here start method will return a list of account which will pass to the scope of execute method ,scope size will depends on your batch size which you will defined while executing the batch class . 

  global class AccountUpdateBatch implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'SELECT Id,Name FROM Account';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Account> scope){
         for(Account acc : scope){
             acc.Name = acc.Name +'UpdatedByBatch';            
         }
         update scope;
    }   
    global void finish(Database.BatchableContext BC){ }
}

How to execute the batch apex :-
In Database class there are two static method
executeBatch(Instance Of class )
excecuteBatch(InstanceOf batch class ,batchsize )

If you want to defined any limit of record to pass into execute method per execution then you need to use second method .Batch size can very from 1-2000.
If you will call first method then it will take the default batch size as 200.

Batch Apex Governor Limits:-
  • All methods in the class must be defined as global or public.
  • Up to 5 batch jobs can be queued or active concurrently.
  • In a running test, you can submit a maximum of 5 batch jobs.
  • The maximum number of batch Apex method executions per a 24-hour period is 250,000 or the number of user licenses in your organization multiplied by 200, whichever is greater.
  • You cannot use the getContent and getContentAsPDF PageReference methods in a batch job.
  • The start, execute, and finish methods can implement up to 10 callouts each.
  • In a running test, you can submit a maximum of 5 batch jobs.
  • 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.
  • Start method can have up to 15 query cursors open at a time per user.
  • Execute and finish methods each have a limit of five open query cursors per user.




8 comments:

  1. Nice Blog, it clear me basic concept of batch apex.
    thank you :)

    ReplyDelete
  2. Hi Manoj

    I want implement Insert records using batch class is there any idea. What i have to write in start method.?

    ReplyDelete
  3. Concise to the point explaination... Thank you, Can you please give some examples of trigger calling batch class, Is that possible?

    ReplyDelete
  4. Hi Manoj, can you give me an example how to use getChildJobId(). It would be very helpful.

    ReplyDelete