You need to sign in to do that
Don't have an account?
![Patrick Conner Patrick Conner](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
Workaround for Aggregate query does not support queryMore()
Below is an outline of a schedulable class I'm trying to deploy. I'm getting the following error on schedule (and in developer console), due to too many IDs in queries: Aggregate query does not support queryMore(), use LIMIT to restrict the results to a single batch.
I can limit the results to 2k, but this renders the class ineffective. I've read of using QueryMore as a workaround, referencing last returned whatID perhaps? I've also seen references to iterable interfaces and @read, but I don't know how to employ either of those.
Any help would be greatly appreciated. Thank you.
global class TaskRetention implements Schedulable { global void execute(SchedulableContext SC) { AggregateResult[] tasksWithin30Days = [Select whatId from task where createddate = Last_n_days:30 group by whatid]; AggregateResult[] noTask30Days = [Select whatId,max(createddate) maxdate from task group by whatid having max(createddate) < Last_n_days:30 ]; Id[] hasTaskWithin30Days = new Id[]{}; Id[] noTasksIn30Days = new Id[]{}; String[] whatIdDateTimeList = new String[]{}; for(AggregateResult ar: tasksWithin30Days ){ hasTaskWithin30Days.add((Id)ar.get('whatId')); } for(AggregateResult ar: noTask30Days){ noTasksIn30Days.add((Id)ar.get('whatId')); whatIdDateTimeList.add(''+ar.get('whatId')+ar.get('maxdate')); } delete [ Select Id from Task where whatId in :hasTaskWithin30Days and createddate< last_n_days:30]; delete [Select Id from Task where whatId in :noTasksin30Days and whatId_createddate__c not in :whatIdDateTimeList]; } }
I can limit the results to 2k, but this renders the class ineffective. I've read of using QueryMore as a workaround, referencing last returned whatID perhaps? I've also seen references to iterable interfaces and @read, but I don't know how to employ either of those.
Any help would be greatly appreciated. Thank you.
Here is my tough, its may work in your case.
- You can use batch apex with Database.Stateful
- Write your own Aggregate logic to perform the bulk data handling
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm