1. Batch Apex
Batch Apex is used to process large volumes of records asynchronously in Salesforce. It divides the records into batches, processes them separately, and ensures governor limits are handled effectively.
Key Points:
- Implemented using
Database.Batchableinterface. - Can process up to 50 million records.
- Divides records into chunks (default 200, customizable).
- Supports
start,execute, andfinishmethods.
Example:
2. @future Methods
@future methods are used to perform operations asynchronously, typically when an operation is resource-intensive or must avoid affecting user interaction.
Key Points:
- Marked with the
@futureannotation. - Methods must be
staticand returnvoid. - Cannot accept
SObjector collection ofSObjectas arguments. - Use when the operation does not require an immediate response.
Example:
3. Queueable Apex
Queueable Apex is similar to @future methods but with more control and flexibility. It supports complex data types and chaining jobs.
Key Points:
- Implements the
Queueableinterface. - Allows you to chain jobs.
- Supports job monitoring through the AsyncApexJob table.
- Useful for scenarios where
@futureis insufficient.
Example:
apex code
public class QueueableExample implements Queueable {
private List<Id> accountIds;
public QueueableExample(List<Id> accountIds) {
this.accountIds = accountIds;
}
public void execute(QueueableContext context) {
List<Account> accounts =
[SELECT Id, Name FROM Account WHERE Id IN :accountIds];
for (Account acc : accounts) {
acc.Description = 'Updated by Queueable Apex';
}
update accounts;
}
}
Interview Questions and Answers
Batch Apex
Q: Why would you use Batch Apex over a normal
forloop?
A: Batch Apex is designed to handle large data volumes while respecting Salesforce governor limits by processing records in manageable chunks.Q: How do you monitor Batch Apex?
A: You can monitor Batch Apex jobs using the Apex Jobs page in Setup or querying the AsyncApexJob table.Scenario:
You have 10 million records in your system and need to apply a discount to customers based on certain criteria. How would you approach it?
Answer:- Use Batch Apex to query the customers in manageable chunks.
- Apply the discount logic in the
executemethod. - Commit updates to the database after processing each chunk.
@future Method
Q: What are the limitations of @future methods?
A:- Cannot return values.
- Limited to 50 calls per transaction.
- Cannot pass
SObjecttypes or collections ofSObjecttypes directly.
Q: Can @future methods be chained?
A: No, @future methods cannot be chained. For chaining, use Queueable Apex.Scenario:
A user updates an Account, and you need to update all related Contacts asynchronously. How would you achieve this?
Answer:- Use an @future method to pass the Account ID.
- Query the related Contacts and update their fields in the @future method.
Queueable Apex
Q: What is the main advantage of Queueable Apex over @future methods?
A:- Supports complex data types.
- Allows job chaining.
- Provides job tracking via the AsyncApexJob table.
Q: How do you chain Queueable jobs?
A:- In the
executemethod of one Queueable class, enqueue another Queueable job usingSystem.enqueueJob(new AnotherQueueableClass()).
- In the
Scenario:
You need to process records in two steps: first update records, then notify users via email. How would you achieve this?
Answer:- Create two Queueable Apex classes: one for updating records and another for sending emails.
- Chain the second job from the first using
System.enqueueJob()in theexecutemethod.
No comments:
Post a Comment