1

I have a requirement to send mass emails to certain Users of current day created records daily. I am unable to do that with below code. Getting error : Aggregate query does not support queryMore()

Please let me know a way out of this. Code:

global class Incidentimp implements Database.Batchable<sObject> {

public string query = 'SELECT DM_Lookup_id__c,DM_Lookup_id__r.name FROM Incident__c where createddate<>today 
                            group by DM_Lookup_id__c,DM_Lookup_id__r.name';

global database.querylocator start(Database.BatchableContext BC)
{
    return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, Sobject[] scope)
{   
    //Map<Inventory_Wishlist__c, Product2> temp = new Map<Inventory_Wishlist__c, Product2>();
   // Set<string> accountIdSet = new Set<string> {};
   // Map<string,string> xyz=new Map<string,string>();
    EmailTemplate emailTemplate = [select Id, Body  from EmailTemplate where DeveloperName = 'POC_DM'];


    for (Incident__c item : (List<Incident__c>)scope) {



                        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
                        email.setToAddresses(new String[] {'saikat.neogy@cognizant.com'});
                       email.setTemplateId(emailTemplate.Id);
                        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
                  }        






}
global void finish(Database.BatchableContext BC) {

}

}
0

2 Answers 2

0

May be your "scope" returning more than 2000 registers. You should read answers for that question > System.QueryException: Aggregate query does not support queryMore() Specially the answer by Daniel

3
  • Hi maximorojr! I am not getting it properly. Can you please guide me here a bit more? Nov 10, 2014 at 10:48
  • Did you get that? salesforce.com/us/developer/docs/apexcode/Content/… Note that queries including an aggregate function don't support queryMore. A run-time exception occurs if you use a query containing an aggregate function that returns more than 2,000 rows in a for loop.
    – maximorojr
    Nov 11, 2014 at 11:29
  • Forgive me, your problem isn't iterating over the scope, it is in your start method that is calling Database.getQueryLocator(query), and it doesn't support aggregated functions too, so you need to figure out a way to remove 'Group By' from your query.
    – maximorojr
    Nov 11, 2014 at 12:15
0

I noticed that you say you want records created on the current day, but you are using '<>' in your query and so getting any records NOT created on the current day. This might explain why you are getting so many results.

Also as the poster above me says, you can't use aggregate query for the scope anyway. You can do the grouping yourself though. You don't need to have DM_Lookup_id__r.name as part of the grouping as it is 1:1 with the Id anyway, so effectively you are left with a set of IDs which you can create from a standard query.

Something like:

public string query = 'SELECT DM_Lookup_id__c, FROM Incident__c where createddate = today';

.......

Set idSet = new Set();

for (Incident__c item : (List)scope) {

idSet.add(item.DM_Lookup_id__c);

}

If you are concerned about the same ID appearing in different batches then you can use the Stateful keyword and build this set over each iteration of the process and then do the emails in the finish method.

Not the answer you're looking for? Browse other questions tagged or ask your own question.