function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
JVIowaJVIowa 

Query Causes Salesforce Internal Error

Any idea why this query would cause an issue?  Create Date should be an indexed field and there is a limit on the query.  Is it because of the Aggregate? Grouping?  Can't figure out what the problem is. If I decrease the limit, that doesn't fix the problem either.  So it has something to do with  the fact that it is attempting this on the task table.  This is a 100 user org that has a fair amount of tasks create in it.  Any ideas?

 

 AggregateResult[] groupedResults = [SELECT COUNT(id), ownerid 
                                    FROM task where custom_field__c != null 
                                    and createddate = LAST_N_DAYS:365 group by ownerid limit 25000];
            integer distinctUsers = 0;
            integer totalTasks = 0;
            set<id> userIds = new set<id>();
            for (AggregateResult ar : groupedResults)  {  
                if(ar.get('ownerid') != null){
                    userIds.add((string)ar.get('ownerid'));
                    distinctUsers++;
                    totalTasks += Integer.valueof(ar.get('expr0'));
                }
                
            }

 

Best Answer chosen by Admin (Salesforce Developers) 
JVIowaJVIowa

I think I might have been running into the 120 second issue.  I was able to get it work by change the query to be a SOQL For Loop.  See below.  I believe this allows the query to broken up into multiple batches.  It did the trick.

 

for (AggregateResult ar : [SELECT COUNT(id), ownerid 
                                    FROM task where custom_field__c != null 
                                    and createddate = LAST_N_DAYS:365 group by ownerid limit 25000])  {  
	//logic here
}

 

All Answers

Saurabh DhobleSaurabh Dhoble

Your variable name looks weird. Shouldn't it say :-

 

[SELECT COUNT(id), ownerid FROM task where custom_field__c != null 
and createddate = :LAST_N_DAYS group by ownerid limit 25000];

 Let me know if I am missing something here.

JVIowaJVIowa

LAST_N_DAYS is not a variable it is a SOQL date literal, see the documentation.  So that is not the issue.

Saurabh DhobleSaurabh Dhoble

Ah, got it, thanks.

 

I ran the same query in my developer console query window and it runs fine :-

select ownerid,count(Id) from task where createddate = LAST_N_DAYS:365 and i__Email__c != null group by ownerid limit 25000

Does it run in your dev console ? It may have something to do with the size of your recordset. I was curious if you put debug statements in the following lines of code to make sure the error is happening in the query itself and not in the code after it.

vishal@forcevishal@force

You can try removing one filter at a time and check if it runs properly, I don't see anything as such. Still just to confirm, you can try this.

JVIowaJVIowa

Interesting that if I adjust the day number down to say 180 or 90 from 365 the query works.  I know it is less data but it seems like 365 should work as well.

Michael_TorchedloMichael_Torchedlo

You said in one comment that if you query a shorter date range the error goes away.  This is just a guess, is it possible that internal error is due to the length of time it is taking to query your tasks?  You said there were a lot of records and in our organization we also have run into similar problem for that reason.

 

"If a SOQL query runs more than 120 seconds, the request can be canceled by Salesforce"

 

(source:  http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_gov_limits.htm  )

 

 

JVIowaJVIowa

I think I might have been running into the 120 second issue.  I was able to get it work by change the query to be a SOQL For Loop.  See below.  I believe this allows the query to broken up into multiple batches.  It did the trick.

 

for (AggregateResult ar : [SELECT COUNT(id), ownerid 
                                    FROM task where custom_field__c != null 
                                    and createddate = LAST_N_DAYS:365 group by ownerid limit 25000])  {  
	//logic here
}

 

This was selected as the best answer