Batch apex is an interface given by salesforce to process large amount of data.
As we know, salesforce provides governer limits on data extraction and DML operations in classes.
In a SOQL query we can get only 50000 records and at once we can use only 10000 records in one DML operation.
Total number of DML operations allowed in apex class is 150 hence we can process upto 1500000 records in an apex class.
But to work on these many records in apex class we need to implement various complex loopings that will be tiring and time consuming job.
Also for large scale industries the amount of data is more than 1500000 hence this approach cannot be used effectively.
To overcome this problem, salesforce provides batch apex interface.
This interface allows to process 50 million records without any complex logic and looping to implement.
As we know, salesforce provides governer limits on data extraction and DML operations in classes.
In a SOQL query we can get only 50000 records and at once we can use only 10000 records in one DML operation.
Total number of DML operations allowed in apex class is 150 hence we can process upto 1500000 records in an apex class.
But to work on these many records in apex class we need to implement various complex loopings that will be tiring and time consuming job.
Also for large scale industries the amount of data is more than 1500000 hence this approach cannot be used effectively.
To overcome this problem, salesforce provides batch apex interface.
This interface allows to process 50 million records without any complex logic and looping to implement.
Batch is divided into 3 methods:-
1. Query Locator :- this methods runs as soon as batch stats running. This method is used to query the required records from database using database.getQueryLocator.
global Database.queryLocator start(database.batchablecontext BC)
{
string query= 'select id, name from account';
return Database.getqueryLocator(query);
}
In the above example first a dynamic query will be created which will then be passed in Database.getqueryLocator as parameter to get records from database.
2. Execute :- after fetching th records from database, the records are divided in batches and then each batch is processed in the execute.
By default the batch size is 200 records but it can increased upto 2000 records in each batch.
For example if the we get 1000 in query locator and batch size is defined as 200 records, then 5 batches will be created and execute method will process 5 times.
global void execute(database.batchablecontext BC , List<SObject> scope)
{
List<Account> acc = scope;
for(Account a: acc)
{
a.name= a.name+'batch class';
}
update acc;
}
In the above example scope variable contains list of records which will be processed in that execute method.
3. Finish :- Once all records are processed and execute is completely over, finish method will be called. This methods can be used in various scenarios like sending the batch status to a user after the records are processed.
global void finish (database.batchablecontext bc)
{
}
This method executes at the end of each batch process.
nice article Alock.... thanks
ReplyDelete