Wednesday, December 23, 2015

Batch Class in Salesforce?

Batch Apex :

By using Batch apex classes we can process the records in batches in asynchronously.
If you have a large number of records to process, for example, for data cleansing or archiving, batch Apex is your solution.
The execution logic of the batch class is called once for each batch of records.
The default batch size is 200 records.
You can also specify a custom batch size. Furthermore, each batch execution is considered a discrete transaction.

With each new batch of records, a new set of governor limits is in effect. In this way, it’s easier to ensure that your code stays within the governor execution limits.

Another benefit of discrete batch transactions is to allow for partial processing of a batch of records in case one batch fails to process successfully, all other batch transactions aren’t affected and aren’t rolled back if they were processed successfully.

We must Write the apex class that must be implements the Database.Batchable Interface.

Batch apex class Syntax:
global Class cleanup implements Database.Batchable< sobject >{
}

Batch apex class contain 3 methods

Start Method:

1.Use the start method to collect the records or objects to be passed to the interface method execute.
2.Start method execute only one time.

Syntax for Start Method:
global (Database.Querylocator|Iterable(< sobject>)start(Database.BatchableContext bc){
}
Database.QueryLocator

1.Database.QueryLocator object when you are using a simple query (SELECT).
2.If you use a querylocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassed. (Default 50,000 It allow up to 50 million records).
Iterable

1.Whenever we write the Complex queries that time the Written type of the method should be Iterable.
2.If you use an iterable, the governor limit for the total number of records retrieved by SOQL queries is still enforced.

Execute Method:

Execute method is called for each batch of records.
Batches of records are not guaranteed to execute in the order they are received from the start method.

Syntax for Execute 

global void execute(Database.BatchableContext BC,List
){ }

Finish Method:

The finish method is called after all batches are processed. Use this method to send confirmation emails or execute post-processing operations.

Each execution of a batch Apex job is considered a discrete transaction.

For example, a batch Apex job that contains 1,000 records and is executed without the optional scope parameter from Database.executeBatch is considered five transactions of 200 records each.

The Apex governor limits are reset for each transaction.
If the first transaction succeeds but the second fails, the database updates made in the first transaction are not rolled back.

Syntax for Finish Method
global void Finish(Database.BatchableContext BC){
}
Returns the ID of the AsyncApexJob object associated with this batch job as a string. Use this method to track the progress of records in the batch job. You can also use this ID with theSystem.abortJob method.

No comments:

Post a Comment