Hello,
I have batch class which run on very large set of records and I am getting soql limit exceptions.
Scenario is:
I have account object (More than 10k records).
Account have child object named XXX__c Object(Per account we have more than 50k records ).
In execute method of batch class I can not access more than 50k records in single transaction.
So, how can I manage it, also I can not write batch on XXX__c object because of business flow.
You can use query more to retrieve a large number of child records: You can use the Database.queryMore method to retrieve a large number of child records in chunks, rather than trying to retrieve all of the records in a single query. This can help you avoid hitting SOQL query limits.
Use the Stateful interface: If you need to maintain state between batches, you can implement the Stateful interface in your batch class. This allows you to store variables in the batch class that can be used to track progress and avoid having to re-query data that has already been processed.
Mark this as best answer if it helps you.
// Query to retrieve the first batch of child records
String soqlQuery = 'SELECT Id, Name FROM XXX__c WHERE AccountId IN :accountIds';
Database.QueryLocator queryLocator = Database.getQueryLocator(soqlQuery);
// Process the first batch of records
for (XXX__c record : (List<XXX__c>)queryLocator) {
// Process the record
}
// Use queryMore to retrieve the remaining batches of records
while (queryLocator.hasNext()) {
for (XXX__c record : (List<XXX__c>)queryLocator.getNextBatch()) {
// Process the record
}
}