Saturday, May 18, 2019

Salesforce Interview Questions on batch apex

  • What is batch apex in salesforce? 


Basically, when we want to deal with a large number of records we go for the batch apex.
The code inside batch class runs asynchronously i.e in a future context.
The governor limit is also more as compared to synchronous code.

When we use batch apex we implement Database.batchable() interface


The Database.batchable() contains three methods that we need to define inside the batch class
and they are,
1)start
2)execute
3)Finish

Start method and finish method are called only once inside the batch class.

Start method collects the data for processing.
The execute method performs operations.
Finish method generally used for sending emails or calling another batch class when the current batch is completed.

For more details visit,

Batch class in Salesforce (Click here)

  • What is database.stateful interface? 


Basically batch class does not maintain its state. If we implements Database.Stateful we can maintained state across transactions.Only instance variable holds values static members does not hold values.If we want to count records as batch proceeds maintaining state is important as after 200 records new transaction will start and members will loose their values.

  • How to write Test class for batch class? 



You need to call batch class from inside of test.starttest() and test.stoptest()
with below syntax,

@istest

public class testclass{

static testmethod void methodname1()
{
test.starttest();

batchClassName obj=new batchClassName ();
Id batchId=Database.executeBatch(obj);

test.stoptest();

}


}

No comments:

Post a Comment