Interview Question on Batch Apex

 

What is Asynchronous Apex?

An asynchronous process is a process or function that executes a task “in the background” without the user having to wait for the task to finish. Asynchronous Apex is used to run processes in a separate thread

One of the main benefits of running asynchronous Apex is higher governor and execution limits.

What are types of Asynchronous apex?

 Asynchronous Apex features their name and use cases are mentioned below:-

Future Methods: it is a basic asynchronous feature, when we make a web call out or when we want to prevent the mixed DML error

Batch Apex: To do bulk processing of records or for the jobs that require larger query result for example processes like Database maintenance jobs

Schedule Apex: This feature is used to schedule the invocation of an apex class at a specific time, this can be a recurring event or a one-time task.

Queueable Apex: When one task is dependent on completion of another task we make use of this asynchronous feature,  Also job chaining and complex type of jobs are achieved using this feature

Why do we use batch apex?

Batch Apex is used to run large jobs (think thousands or millions of records!) that would exceed normal processing limits

What interface will you use for batch apex?

It is Database.Batchable interface

What are the methods used in batch apex?

three methods are used:

Start  – This method is called once at the beginning of a Batch Apex job and returns either a Database.QueryLocator object or an Iterable that contains the records or objects passed to the job.

Execute – Performs the actual processing for each chunk or “batch” of data passed to the method. The default batch size is 200 records. Batches of records are not guaranteed to execute in the order they are received from the start method.

Finish -finish Used to execute post-processing operations (for example, sending an email) and is called once after all batches are processed.

What is the difference between queryLocator object and Iterable used in batch apex?

QueryLocator object, the governor limit for the total number of records retrieved by SOQL queries is bypassed and you can query up to 50 million records. However, with an Iterable, the governor limit for the total number of records retrieved by SOQL queries is still enforced.

What are the parameters passed in the execute method?

This method takes the following:

A reference to the Database.BatchableContext object.

A list of sObjects, such as List<sObject>, or a list of parameterized types. If you are using a Database.QueryLocator, use the returned list.

How to invoke batch Class?

To invoke a batch class, simply instantiate it and then call Database.executeBatch with the instance:

You can also optionally pass a second scope parameter to specify the number of records that should be passed into the execute method for each batch

What is the state of batch apex?

Batch Apex is typically stateless. Each execution of a batch Apex job is considered a discrete transaction. For example, a batch Apex job that contains 1,000 records and uses the default batch size is considered five transactions of 200 records each.

What is the use of Database.Stateful?

If you specify Database.Stateful in the class definition, you can maintain state across all transactions. When using Database.Stateful, only instance member variables retain their values between transactions. Maintaining state is useful for counting or summarizing records as they’re processed. For example, we’ll be updating contact records in our batch job and want to keep track of the total records affected so we can include it in the notification email.

When to use batch apex instead of Queueable Apex?

Only you should use Batch Apex if you have more than one batch of records. If you don’t have enough records to run more than one batch, you should use Queueable Apex.

How to monitor Batch job?

Go to Setup->Apex Job page

 How to use HTTP Callouts in batch class?

To use HTTP Callouts in batch class we need to use Database.allowcallouts in interface.

How to schedule a batch?

Using schedule apex we can schedule a batch

If a batch is having 200 records and 1 record fails what will happen?

If any record fails all 200 record will fail but next batch will get executed

Can we call the batch into another batch apex?

Yes, we can call from the finish method.

Can we call batch apex into another batch in execute method?

Only in batch class finish method, We can call another batch class. If you will call another batch class from batch class execute and start method, then Salesforce will throw below runtime error.

System.AsyncException: Database.executeBatch cannot be called from a batch start, batch execute, or future method.

Can we call the batch apex from triggers in salesforce?

Yes, it is possible. We can call a batch apex from trigger but we should always keep in mind that we should not call batch apex from trigger each time as this will exceeds the governor limit this is because of the reason that we can only have 5 apex jobs queued or executing at a time.

Can we call a webservice callout from batch apex?

To make a Webservice callout in batch Apex, we have to implement Database.AllowsCallouts interface.

How many times start,execute,finish methods will execute in batch apex?

Start method,finish method one time, execute method it depends on requirement. Based on the batch size and data retrieved in Start method.

What is the Batch executions limit per day?

The maximum number of batch executions is 250,000 per 24 hours.

Can we call the future method in batch class?

No,we can’t call.

Can I call Queueable from a batch?

Yes, But you’re limited to just one System.enqueueJob call per execute in the Database.Batchable class. Salesforce has imposed this limitation to prevent explosive execution. 

How to test batch apex?

 Code is run between test.startTest and test.stopTest. Any asynchronous code included within Test.startTest and Test.stopTest is executed synchronously after Test.stopTest.

How many records we can insert while testing batch apex?

We have to make sure that the number of records inserted is less than or equal to the batch size of 200 because test methods can execute only one batch. We must also ensure that the Iterable returned by the start method matches the batch size.

Did you enjoy this article?
Signup today and receive free updates straight in your inbox.
I agree to have my personal information transfered to MailChimp ( more information )
50% LikesVS
50% Dislikes