When working with Apex batch processing in Salesforce, you might encounter scenarios where you need to handle and process multiple records asynchronously. One of the key components of batch processing in Apex is handling the results of these batch actions. In Salesforce, the term “Apex batch action results” refers to the outcome of batch jobs, specifically the results generated after executing the batch logic. These results might include the success or failure of individual operations (like insertions, updates, or deletions) or any exceptions that were thrown during execution.
The “Request Order” aspect comes into play when you want to maintain or control the sequence in which the batch actions are processed and sorted in relation to their execution. It might be useful in scenarios where the order of execution or result processing is important, such as when you need to keep track of record IDs or ensure that related records are processed in a particular order.
Salesforce doesn’t directly offer an out-of-the-box feature to sort batch action results by request order. However, you can manually sort the results by using custom logic in your execute()
method or by utilizing a custom object or a list to track the original request order of the records.
Here’s an example of how you could structure your batch class to track and sort the results by the request order:
public class BatchActionExample implements Database.Batchable<SObject> {
public String query;
public BatchActionExample(String q) {
query = q;
}
public Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(query);
}
public void execute(Database.BatchableContext BC, List<SObject> scope) {
// Example list to track records by their request order
List<BatchResult> results = new List<BatchResult>();
for (SObject record : scope) {
// Process the record
try {
// Your logic here (e.g., update, delete)
update record;
results.add(new BatchResult(record.Id, 'Success'));
} catch (Exception e) {
results.add(new BatchResult(record.Id, 'Failed', e.getMessage()));
}
}
// Now, sort the results by request order
results.sort((r1, r2) -> r1.requestOrder.compareTo(r2.requestOrder));
// Process the sorted results (you can log, notify, or take further actions)
for (BatchResult result : results) {
System.debug('Record ' + result.recordId + ' processed with status ' + result.status);
}
}
public void finish(Database.BatchableContext BC) {
// Final processing after the batch has been completed
}
public class BatchResult {
public String recordId;
public String status;
public String errorMessage;
public Integer requestOrder;
public BatchResult(String id, String status, String errorMessage = null) {
this.recordId = id;
this.status = status;
this.errorMessage = errorMessage;
this.requestOrder = Integer.valueOf(recordId); // Example logic for tracking order
}
}
}
Explanation:
- The
BatchActionExample
class implements theDatabase.Batchable
interface and processes a list of records (SObjects). - In the
execute()
method, a custom classBatchResult
is used to track each record’s ID, status, and any error message, along with arequestOrder
to keep track of the order. - After processing the batch, the results are sorted by the
requestOrder
field using a custom sorting mechanism. - Finally, you can use the sorted results to perform additional processing or logging as needed.
By sorting the results manually as shown, you can control the processing order based on your own tracking logic. This is essential when the request order impacts downstream operations, such as related record processing or when specific sequencing is necessary.
Our Salesforce training in India provides comprehensive learning with practical exposure to real-world projects and expert guidance. Designed for all skill levels, this course equips you with the expertise to excel in Salesforce CRM. Enroll today and take the first step toward a successful career in Salesforce!
The post What Are Apex Batch Action Results? appeared first on Salesforce Online Training.