0% found this document useful (0 votes)
7 views

_Asynchronous Apex Interview Questions(Q&A)_

The document outlines key concepts and best practices related to Batch Apex in Salesforce, including the use of Database.BatchableContext, the differences between Database.QueryLocator and Iterable<SObject>, and the handling of callouts within batch jobs. It also explains the implications of uncommitted transactions, the nature of System.FinalException, and the limitations of batch jobs and asynchronous methods like future methods and Queueable Apex. Additionally, it discusses how to manage batch job failures and the independent processing of transactions in Batch Apex.

Uploaded by

harshak0672
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)
7 views

_Asynchronous Apex Interview Questions(Q&A)_

The document outlines key concepts and best practices related to Batch Apex in Salesforce, including the use of Database.BatchableContext, the differences between Database.QueryLocator and Iterable<SObject>, and the handling of callouts within batch jobs. It also explains the implications of uncommitted transactions, the nature of System.FinalException, and the limitations of batch jobs and asynchronous methods like future methods and Queueable Apex. Additionally, it discusses how to manage batch job failures and the independent processing of transactions in Batch Apex.

Uploaded by

harshak0672
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/ 7

Asynchronous Apex interview questions

what is the use of database.BatchableContext ?

“Database.BatchableContext” is an interface used to access information


about a currently running batch job, particularly its ID, allowing you to
track the progress and manage large data sets by processing them in
smaller batches without exceeding governor limits

Difference Between Database.QueryLocator and


Iterable<SObject> in Batch Apex

✅ Use Database.QueryLocator when handling a large volume of records


efficiently.
✅ Use Iterable<SObject> when you need custom logic before passing
records to Batch Apex.

Feature Database.QueryLocator Iterable<SObject>


Handling large data (up to 50 Handling small datasets (up
Best For
million records) to 50,000 records)
Uses a custom list of
How It Uses a SOQL query to fetch
records, giving more
Works records in chunks
control
More efficient for bulk More flexible but limited in
Performance
processing size
When you need custom
When you need to process
Use Case filtering or complex queries
millions of records efficiently
before processing

Is it possible to do callouts in Bach APEX?

Yes, you can perform HTTP callouts in Batch Apex in Salesforce. To do


this, your batch class must implement the “Database.AllowsCallouts”
interface. This interface permits the batch job to make callouts during its
execution.

Key Considerations:

Callout Limits: Each execution of the start, execute, and finish methods
can perform up to 100 callouts.
Scope Size: If your batch job processes a large number of records, be
mindful of the scope size to avoid exceeding callout limits. For instance,
if your scope size is 200, only the first 100 records will successfully
make callouts. Adjusting the scope size can help manage this.

what is mean by database.platform events why it is used ?

BatchApexErrorEvent:

Purpose: This standard platform event is automatically fired when


an unhandled exception occurs in a Batch Apex job. It captures
detailed information about the error, including the exception
message, stack trace, and the context in which the error occurred.

Implementation: To utilize this feature, your Batch Apex class


must implement the “Database.RaisesPlatformEvents” interface.

what do you mean by uncommitted transaction pending in


batch.how to slove this

In Salesforce Batch Apex, encountering the error message "You have


uncommitted work pending. Please commit or rollback before calling
out." indicates that a callout (external HTTP request) is being attempted
after performing Data Manipulation Language (DML) operations within
the same transaction. Salesforce enforces a strict order: callouts must
be executed before any DML operations in a single transaction.

Understanding the Error:

 Transaction Sequence: If your Batch Apex execute method


performs DML operations (like insert, update, or delete) before
making a callout, Salesforce will prevent the callout to maintain
data integrity and system stability.

Solutions to Resolve the Error:

Perform Callouts First: Structure your code to execute all necessary


callouts before any DML operations within the same transaction. This
approach aligns with Salesforce's requirement to prevent uncommitted
work during callouts.

what is mean by system .final exception


In Salesforce Apex, a “System.FinalException” is thrown when an
operation attempts to modify data that is immutable or read-only in the
current context. A common scenario where this exception occurs is
within "after" triggers.

Understanding the "Record is Read-Only" Exception:

 After Triggers: In an after insert or after update trigger, the


records in Trigger.new are read-only because they have
already been committed to the database. Attempting to
modify these records directly will result in a
System.FinalException with the message "Record is read-
only."

Best Practices to Avoid This Exception:

Use Before Triggers for Modifications:

o Perform record modifications in before insert or before


update triggers. In these contexts, Trigger.new is writable,
allowing you to make changes before the records are saved
to the database.

can we able to change the batch jobs

Yes, you can change batch jobs in Salesforce. You can reschedule,
cancel, or delete batch jobs. You can also share batch jobs with other
Salesforce organizations.

Batch job limit

Explanation
Batch job limit: Salesforce limits the number of concurrent Apex batch jobs
to five.
Batch size: Each batch can have up to 2,000 records.
Total records processed: Salesforce limits the total number of records
processed to 50 million per 24 hours.
Batch job starts: Salesforce limits the number of batch job starts to 250,000
per 24 hours.
What happens if a batch job fails?
1. If a record in a batch fails, the entire batch rolls back.
2. The batch job is marked as Failed and immediately terminated if more
than 50 million records are returned.

What do you mean by “Apex Flex Queue”


The Apex Flex Queue is a queue in Salesforce that holds jobs that are
submitted for execution but are not immediately processed.

Limits: The Flex Queue can hold up to 100 batch jobs in the Holding
status. Attempting to add more will result in an error

How many callouts to external services can a single Apex


transaction perform, and what strategies can be employed to
manage this limit effectively?

A single transaction can perform up to 100 callouts to external services.


To manage this limit effectively:

Batch Apex: Implement Batch Apex to process records in


manageable chunks, ensuring that each batch execution stays
within the callout limits.

Asynchronous Processing: Use asynchronous methods, such as


future methods or Queueable Apex, to handle callouts without
blocking the main execution thread.

why future method will not accept objects as a parameter

Methods with the future annotation can't take sObjects or objects as


arguments. The reason why sObjects can't be passed as arguments to
future methods is because the sObject can change between the time
you call the method and the time it executes.

Can we call the future method from another future method?


No, you cannot call a future method from another future method in
Salesforce. This is because both future methods and batch classes are
asynchronous, so you cannot call an asynchronous method from another
asynchronous method.
can we able to queue future method in salesforce

Yes, in Salesforce, you can "queue" a future method, meaning you can
initiate a future method which will be added to an asynchronous queue and
executed when system resources are available, effectively allowing it to
run in the background without blocking the current user's
interaction; however, you cannot directly chain future methods together as
they are not designed for complex job dependencies, and for that, you
should use Queueable Apex instead.

Is it possible to invoke a future method from within a


Queueable or Batch Apex class in Salesforce? If not, what are
the recommended alternatives?

In Salesforce, invoking a future method from within a Queueable or Batch


Apex class is not supported. This restriction is due to the asynchronous
nature of these processes, which can lead to complexities and potential
issues in managing asynchronous job execution.

Recommended Alternatives:

Queueable Apex:Instead of using future methods, implement


Queueable Apex. Queueable classes provide more flexibility and allow
for complex data types, including sObjects, to be passed as
parameters. They also support job chaining, enabling one
asynchronous job to trigger another upon completion.

Chaining Queueable Jobs:If you need to perform multiple


asynchronous operations sequentially, you can chain Queueable

Can we call queueable method from future method?

Yes, you can call a Queueable method from a Future method in


Salesforce; you can enqueue a Queueable job within the execution of a
Future method, effectively allowing you to chain asynchronous
operations by calling a Queueable class from within your Future method
call.

how many times we change the job in queueable apex salesforce

In Salesforce, you can chain up to 50 queueable jobs in a single


transaction. However, the maximum stack depth for chained jobs in
Developer Edition and Trial organizations is 5. This means that you can
chain jobs four times.

Can you explain the differences between Future methods and


Queueable Apex in Salesforce?

In Salesforce, both Future methods and Queueable Apex are used to


perform asynchronous operations, but they have distinct differences in
terms of functionality and use cases:

Parameter Support:

1. Future Methods: Accept only primitive data types (such as


Integer, String, Boolean) as parameters. They do not
support complex data types like sObjects or custom Apex
types.
2. Queueable Apex: Allows the use of complex data types,
including sObjects and custom Apex types, as member
variables. This flexibility enables more advanced
processing scenarios.

Job Monitoring:

1. Future Methods: Do not provide a mechanism to track the


status of the asynchronous job once it's queued.
2. Queueable Apex: Generates a job ID upon enqueuing,
allowing developers to monitor the job's progress and
status through the AsyncApexJob object.

Chaining Capabilities:

1. Future Methods: Cannot be called from another future


method or from a Batch Apex context, limiting their ability
to chain asynchronous operations.
2. Queueable Apex: Supports job chaining, enabling one
Queueable job to enqueue another. However, there is a limit
to the depth of chained jobs to prevent excessive resource
consumption.

Use Cases:

1. Future Methods: Suitable for simple, fire-and-forget


operations where tracking and complex parameter passing
are not required.
2. Queueable Apex: Ideal for more complex asynchronous
processing needs, such as operations requiring complex
data types, job monitoring, or chaining multiple jobs.
Can we use queueable in batch apex

Yes, in Salesforce, you can invoke a Queueable Apex job from


within a Batch Apex class, specifically from its execute or finish
methods.

In Salesforce Batch Apex, if you have a total of 600 records to


process with a batch size of 200, resulting in three
transactions, and the second transaction fails while the first
and third succeed, what happens to the records processed in
each transaction?

In Salesforce Batch Apex, each batch of records is processed in a


separate transaction. Given your scenario:

First Batch (Records 1–200): Processed in the first transaction.


Since this transaction is successful, all operations performed on
these records are committed to the database.

Second Batch (Records 201–400): Processed in the second


transaction. If this transaction fails, all operations performed on
these records are rolled back, meaning no changes are committed
to the database for this batch.

Third Batch (Records 401–600): Processed in the third transaction.


Since this transaction is successful, all operations performed on
these records are committed to the database.

Therefore, the failure of the second transaction does not affect the
successful processing and committed changes of the first and third
transactions. Each batch operates independently, ensuring that a failure
in one batch does not impact the others.

You might also like