Asynchronous Apex
1) Batch Apex 2) Schedule Apex 3) Future Methods 4) Queueable ApexBatch Apex:
Batch Apex can be used to process records step by step. In order to Implement Batch Apex an Apex Class has to implement Database.Batchable<sObject> Interface. There are 3 methods in a batch apex class: 1) Start method 2) Execute Method 3) Finish Method Start method is called at the start of the batch Apex. In Start method, we will prepare the data or query the data from database and send this to execute method. The return type of Start Method is Iterable or Database.querylocator We can Process up to 50,000 Records When the return Type is taken as 50,000. We can Process up to 50 Million Records When the return Type is taken as Database.QueryLocator. Execute method is called after the start method, it receives the list of record which can be processed as required. The return type of Execute Method is Void. Finish method is called at last after all the batch processes are finished. Finish method can be left blank if not required or can be used to send the email confirmations. The return type of Finish Method is Void. The default Batch size is 200. The minimum size of a Batch is 1. The maximum size of a Batch is 2000. Batch Apex is used for Processing Huge Amount of Data. 10 Lakh Records Batches 200 Records ------------------------------------------------------------------------------- If the Annual Revenue of an Account is more than 50 Lakhs Update those account's rating to Hot. global class UpdateAccountBatch implements Database.batchable<sObject>{ global Database.QueryLocator start(Database.BatchableContext bc) { string query = 'SELECT Id,Name,Rating,AnnualRevenue FROM Account WHERE AnnualRevenue > 5000000'; return database.getQueryLocator(query); } global void execute(Database.BatchableContext bc, List<Account> accounts){ List<Account> acclist = new List<Account>(); for(Account a: accounts){ a.rating = 'Hot'; acclist.add(a); } update acclist; } global void finish(Database.BatchableContext bc) { } } --------------------------------------------------------------------------- @isTest public class UpdateAccountBatchTest { public static testMethod void unitTest(){ // Query Used in the Batch Job string query = 'SELECT Id,Name,Rating,AnnualRevenue FROM Account WHERE AnnualRevenue > 5000000'; List<Account> accounts = new List<Account>(); // Create 200 Test Records to test the Batch Class for(Integer i = 0; i < 200 ; i++){ Account a = new Account(); a.name = 'Account' + i; a.Rating = 'Cold'; a.AnnualRevenue = 6000000; a.Phone = '09876543210'; accounts.add(a); } if(accounts != Null && accounts.size() > 0){ insert accounts; } Test.startTest(); // Create an Instance of the Batch Class UpdateAccountBatch obj = new UpdateAccountBatch(); // Call the Batch Class Database.executeBatch(obj,200); Test.stopTest(); // Verify the Results Integer i = [SELECT COUNT() FROM Account WHERE Rating = 'Hot']; system.assertEquals(200, i); } } -------------------------------------------------------------------------------------- // Create an Instance of the Batch Class UpdateAccountBatch obj = new UpdateAccountBatch(); // Call the Batch Database.executeBatch(obj,200);
========================================================================
Interview Questions::
Can we call a batch class from another batch class?
Yes, we can call one batch class from another batch class but only if it is called from its finish method.
Can we call a Future method from batch class?
no, we cannot directly call a future method from a batch class.
What is Database.Stateful used for in batch class?
As batch class runs asynchronously, to maintain the state of a variable across each batch we implement Database.Stateful.
How many can concurrent batch jobs be added to the queue?
At most, there can be 5 bath jobs queued at a time.
How can we track the status of the current running batch job?
The job Id returned by the batchable context variable helps us in finding the status of a batch through AsyncApexJob.Status.
Q . What is the purpose of start method in Batch Class?
Start object returns the list or group of records on which we are going to make changes.
What is database.getQuerylocator and what are its advantages?
It is used to fetch records from the database, if used in batch class it can fetch up to 50 millions records.
--Manu
No comments:
Post a Comment