Salesforce Interview Question on Batch class
What is Batch Apex?
It is a class which is capable of accepting records in bulk and then depending upon the batch size the group of records are broken into Batches and are processed asynchronously.
When should we use a batch class?
We should batch apex when we are dealing with records in bulk mainly the record size above 10000 and cannot be handled by the normal DML operations.
What is the basic structure of a Batch class?
the basic structure of a batch consists of three methods:
Describe each method of a batch class?
Syntax: global Database.QueryLocator start(Database.BatchableContext BC)
This method receives the batchable context variable as an argument and returns a query locator variable.
Syntax: global void execute(Database.BatchableContext BC, List<Account> scope)
This method receives the batchable context variable and an additional argument as scope which of list of subject type we are working with.
Syntax: global void finish(Database.BatchableContext BC)
This method receives the batchable context variable and is the last method which gets executed after all the batches have been processed, we can perform all the post execute logic in this method like sending an email etc.
What is a scheduler class?
A scheduler class implements the Schedulable interface and have to implement its execute method.
how to schedule a batch class ?
there are two ways to schedule a batch class:-
- Using developer console
- Using System Scheduler
By Developer console we have to pass a cron variable like below:-
AScheduleClass A = new AScheduleClass();
String schedule = ’20 30 8 10 2 ?’; // cron object
String jobID = system.schedule(‘Merge Job’, schedule, A);
Via System Scheduler:
- Go to setup and in quick search bar type schedule. Then click on schedule apex.
- Select schedule created and set the time with help of the Salesforce UI.
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.
******* More questions to come
Want to learn more about batch Classes and Asynchronous Apex have a look at link below