You are currently viewing Salesforce Developer Interview Questions Part10 –  Batch Apex

Salesforce Developer Interview Questions Part10 –  Batch Apex

Salesforce Developer Interview Questions – Asynchronous Apex – Batch Apex

 Batch Apex

104. What is a batch apex?

Answer:

Batch apex is one of the asynchronous apex features or type.

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

Using Batch Apex, we can process records asynchronously in batches (hence the name, “Batch Apex”) to stay within platform limits.

If we have a lot of records to process, for example, data cleansing or archiving, Batch Apex is probably the solution

105. How to use a batch apex?

Answer:

To use batch apex, write an apex class that implements the salesforce provided interface Database.Batchable and then invoke the class programmatically.

106. Why do we implement Database.batchable interface or what happens when we implement Database.batchable interface?

Answer:

Database.Batchable interface contains three methods that must be implemented.

Start()

Execute()

Finish()

These three methods are mandatory if we use Database.Batchable interface. If we do not implement any one of the three methods then the class throws an error.


107. Can you explain about three methods of batch apex?

Answer:

Start method: It is used to collect the records of objects to pass to the interface method “execute”, call the start method at the beginning of a batch apex job. This method returns either a Database.QueryLocator object or an iterable that contains the records or objects passed to the job

Execute method: It is used to do the required processing for each chunk of data. This method is called for each batch of records that you pass to it.

Batches of records tend to execute in the order in which they’re received from the “start” method. However, the order in which batches or records execute depends on various factors. The order of execution isn’t guaranteed.

Finish method: This method is called after all batches are processed. It can be used to send confirmation emails or execute post-processing operations.

108. Can you write a sample Batch apex class?

Answer:

109. Explain about Database.Querylocator in batch apex?

Answer:

Database.querylocator can be used for simple query (SELECT) to generate the scope of the objects in the batch job.

With Querylocator the governor limit for the total number of records retrieved by SOQL queries is bypassed.

When using querylocator the maximum number of records can be retrieved is up to 50 millions.

110. Explain about Iterable in batch apex?

Answer:

Iterable is used when we have to create a more complex scope for the batch job.

Example if the batch class needs to operate on data obtained from two unrelated objects, then we can create a custom iterable class and return combined data from both objects as a list of iterable from start method.

With Iterable we can pass both the objects or records to the execute method for processing.

With iterable the governor limit for the total number for records retrieved by SOQL queries is still enforced which is up to 50 thousand records

111. How to invoke a batch Class?

Answer: 

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

Optionally we can pass a second scope parameter to specify the number of records that should be passed into the execute method for each batch.

112. Can we track the progress of the batch job?

Answer:

Yes. Each batch apex invocation creates an AsyncApexJob record so that we can track the job’s progress.

We can view the progress via SOQL or manage you job in the Apex Job Queue,

SOQL :

Apex Job Queue : Step → Jobs → Apex Jobs

This Post Has 4 Comments

  1. N

    These are very important points and you are one of a kind who makes the information reachable and understandable. Please keep doing this kind of videos and blogs. I am paitiently waiting for LWC and Integration after you complete the Async Apex. Godd luck with your work!

    1. Ashfaq Mohammed

      Thanks for your patience, I am working on LWC interview question series.

  2. sonali

    All Videos and blogs made by you are helpful to me in understanding interview preparation. Your communication in videos is very effective. I am just waiting for integration and LWC. Appreciate your efforts, Sir.

    1. Ashfaq Mohammed

      Thanks for your kind words, I will try to make integration and LWC videos soon

Leave a Reply