0% found this document useful (0 votes)
49 views29 pages

Batch Apex Interview Questions

Uploaded by

shaamidreez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views29 pages

Batch Apex Interview Questions

Uploaded by

shaamidreez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Notes

Batch Apex

Interview
Questions

Sweety Kumari
Notes
Basic Level Interview Questions

1. What is Batch Apex?


Answer: Batch Apex is a feature in
Salesforce that allows developers to
process large volumes of data by
breaking them into manageable
chunks. This is done
asynchronously, which means the
processing happens in the
background. It is useful for
operations that need to process
thousands or millions of records.
2. What is the default batch size in
Batch Apex?
Answer: The default batch size is
200 records.
Notes

3. How do you define a Batch Apex


class?
Answer: To define a Batch Apex
class, you need to implement the
Database.Batchable interface. Here
is a basic structure:
Notes
4. What are the three methods in the
Database.Batchable interface?
Answer: The three methods in the
Database.Batchable interface are:
start: This method is used to
collect the records or objects to
be passed to the execute method.
It returns a
Database.QueryLocator or an
Iterable.
execute: This method is invoked
for each batch of records passed
from the start method.
finish: This method is called after
all batches are processed. It is
used for any post-processing or
cleanup.
Notes
5. How do you execute a Batch Apex
job?
Answer: You can execute a Batch
Apex job using the
Database.executeBatch method.

You can also specify the batch size:


Notes
Intermediate Level Interview
Questions
6. Can you schedule a Batch Apex
job? If so, how?
Answer: Yes, you can schedule a
Batch Apex job using the
System.schedule method along with
the Schedulable interface. Here’s an
example:
Notes
7. What is the maximum number of
batch Apex jobs that can be queued
or active at one time?
Answer: The maximum number of
batch Apex jobs that can be queued
or active at one time is 100.

8. How do you handle large data


volumes in Batch Apex?
Answer: To handle large data
volumes in Batch Apex, you can:
Optimize the start method query
to retrieve only necessary fields.
Use selective queries with
indexed fields.
Consider using the
Database.Stateful interface if
you need to maintain state
across batches.
Notes
9. Explain the Database.Stateful
interface in Batch Apex
Answer: The Database.Stateful
interface allows a Batch Apex class
to maintain state across transaction
boundaries. For example, you can
use it to keep track of the number of
records processed or to accumulate
results across different batches.
Notes

10. What are some common use


cases for Batch Apex?
Answer: Common use cases for Batch
Apex include:
Data archiving and cleanup.
Processing large sets of records
for data migration.
Performing complex calculations
on large datasets.
Integrating with external systems
where large data volumes need to
be processed.
Recalculating summary data.
Notes
Advanced Level Interview Questions

11. How can you monitor the


progress of a Batch Apex job?
Answer: You can monitor the
progress of a Batch Apex job by
querying the AsyncApexJob object.
Here’s an example:
Notes
12. How do you handle errors in
Batch Apex?
Answer: To handle errors in Batch
Apex, you can:
Implement try-catch blocks
within the execute method to
catch exceptions.
Log errors to a custom object or
use the System.debug method to
log error details.
Use the Database.SaveResult
class to check for DML operation
results and handle errors
accordingly.
Use the
Database.RaisesPlatformEvents
interface to publish events on
batch completion or failure.
Notes
13. Explain how the finish method
can be used for chaining batch jobs.
Answer: The finish method can be
used to chain batch jobs by starting
a new batch job after the current one
finishes. Here’s an example:
Notes
14. What are the governor limits
specific to Batch Apex?
Answer: Some of the governor limits
specific to Batch Apex are:
Up to 200 records can be
processed per batch.
A maximum of 5 active or queued
batch jobs per Apex user.
A maximum of 100 active or
queued batch jobs for the entire
organization.
1,000 batch executions per 24
hours.
50 million records can be
processed in a single batch
execution.
Notes
15. Can you implement Batch Apex
with Platform Events? If so, how?
Answer: Yes, you can implement
Batch Apex with Platform Events.
This allows you to publish events at
different stages of the batch
processing.
Here’s an example:
Notes

15. how to get successfully


processed record ids in batch apex
?
Answer: To get the IDs of the
successfully processed records in
Batch Apex, you can use the
Database.SaveResult class. This
class provides details about the
success or failure of DML
operations performed within the
execute method.
Here is an example of how you can
capture the IDs of successfully
processed records:
Notes
Notes
16. what are batch apex limit in
salesforce ?
Answer: Batch Apex in Salesforce
is a powerful tool, but it comes
with several governor limits to
ensure the platform remains
performant and fair for all users.
Here are the key limits specific to
Batch Apex:
1. Batch Size Limits:
The maximum number of
records that can be
processed per batch is
2000.
The default batch size is
200 records.
Notes
2. Batch Jobs Limits:
Up to 5 active or queued
batch jobs per Apex user.
Up to 100 active or queued
batch jobs for the entire
organization.
Up to 1,000 batch jobs can
be executed per 24 hours.
A batch Apex job can have up
to 50 million records
processed in a single
execution.
Notes
17. There is one code for callout and
working fine when running from
anonymous window.When i put the
same code in execute method of
Batch Apex class, code is not
working. What might be the reason?
Answer: We need to implement ‘
Database.AllowsCallouts’ interface.
in batch Apex.

18.how many callouts we can make


from batch apex ?
Answer: Callout Limit per
Transaction:
You can make up to 100 callouts
per transaction.
Notes
Example Scenario:
Assuming you want to process
10,000 records with a batch size of
100 records, and you need to make
a callout for each record:
Total Records: 10,000
Batch Size: 100
Total Batches: 10,000 / 100 =
100
Callouts per Batch: Up to 100
This setup will allow you to make a
callout for each record without
exceeding the 100 callouts per
transaction limit, resulting in a total
of 10,000 callouts for the entire
batch job.
Notes
19. In a Batch Apex job, if an error
occurs while processing a batch,
the entire batch is rolled back. How
can you ensure that only the
problematic records are rolled back
while allowing the successful
processing of the remaining
records?
Answer: We can handle this
scenario by utilizing the Database
method available. Additionally,
below is a sample of how we can
address any errors encountered by
a batch.
Notes
Notes
20. What is the difference between
Database.QueryLocator and
Iterable
in batch apex?
Asnwer: In Salesforce Batch Apex,
Database.QueryLocator and
Iterable are two different ways to
specify the scope of records to
process.
Here’s a detailed comparison of the
two:
Database.QueryLocator :
Database.QueryLocator is used to
define the scope of records using a
SOQL query. It allows the batch job
to handle large sets of data by
querying them in chunks.
Notes
Usage:
Commonly used when dealing with
large volumes of data that exceed
the governor limits for heap size or
query results.
Features:
1. Handles Large Data Sets:
Can process up to 50
million records.
Salesforce manages the
chunking of records behind
the scenes.
2. SOQL Query:
Requires a SOQL query to
retrieve the records.
Can take advantage of
query optimizations and
indexing.
Notes
3. Automatic Chunking:
Salesforce automatically
chunks the data into
manageable pieces, making it
efficient to handle large data
volumes.

Example:
Notes
Iterable:
Iterable allows you to define the
scope of records using an Apex
Iterable, which is a collection of
objects that can be iterated over.

Usage:
Commonly used when you have a
pre-defined collection of records or
when you need to process a
complex data set that is not easily
retrieved by a single SOQL query.
Notes
Features:
1. Custom Collections:
Useful for complex data sets
that require custom logic to
gather records.
Can use any collection type
that implements the Iterable
interface, such as a List.
2. Flexibility:
Allows for more flexibility in
defining the data set.
You can mix different object
types and apply custom
filtering or processing logic.
3. Limited to Heap Size:
Must be mindful of governor
limits, such as heap size,
when using large collections
Notes
Example:
Notes
Bonus Question:
Can we create batch apex with more
than one execute method ?
Answer :
Try it now and you will get your
answer.

You might also like