List<Map<String, String>> objectQueriesList = new List<Map<String, String>>{
new Map<String, String>{'Account' => 'SELECT Id, Name FROM Account'},
new Map<String, String>{'Contact' => 'SELECT Id, FirstName, LastName FROM Contact'},
// Add more maps as needed
};
CustomerDeletionBatch initialBatch = new CustomerDeletionBatch(objectQueriesList);
Database.executeBatch(initialBatch);
global class CustomerDeletionBatch implements Database.Batchable<sObject>, Database.Stateful {
private List<Map<String, String>> objectQueriesList;
private Integer currentIndex;
global CustomerDeletionBatch(List<Map<String, String>> objectQueriesList) {
this.objectQueriesList = objectQueriesList;
this.currentIndex = 0;
}
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = getQueryFromList();
if (query != null) {
System.debug('query' + query);
return Database.getQueryLocator(query);
}
return null;
}
global void execute(Database.BatchableContext BC, List<sObject> scope) {
for (sObject record : scope) {
System.debug('Record ID: ' + record.Id);
// Add your custom logic here to process the records
}
}
global void finish(Database.BatchableContext BC) {
if (currentIndex < objectQueriesList.size()) {
String nextQuery = getQueryFromList();
if (nextQuery != null) {
// Call the batch again with the next set of object queries
CustomerDeletionBatch nextBatch = new CustomerDeletionBatch(objectQueriesList);
Database.executeBatch(nextBatch);
System.debug('nextBatch' + nextBatch);
}
}
}
private String getQueryFromList() {
if (objectQueriesList != null && currentIndex < objectQueriesList.size()) {
Map<String, String> currentObjectQueries = objectQueriesList[currentIndex];
List<String> parentObjectApiNames = new List<String>(currentObjectQueries.keySet());
if (parentObjectApiNames.size() > 0) {
String parentObjectApiName = parentObjectApiNames[0];
System.debug('parentObjectApiName' + parentObjectApiName);
currentIndex++;
return currentObjectQueries.get(parentObjectApiName);
}
}
return null;
}
}
Oneplus Audio Days
0 Comments