You need to sign in to do that
Don't have an account?
JVIowa
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')); } }
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.
All Answers
Your variable name looks weird. Shouldn't it say :-
Let me know if I am missing something here.
LAST_N_DAYS is not a variable it is a SOQL date literal, see the documentation. So that is not the issue.
Ah, got it, thanks.
I ran the same query in my developer console query window and it runs fine :-
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.
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.
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.
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 )
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.