You need to sign in to do that
Don't have an account?
MikeGill
Too many SQL Queries with basic task trigger
Hi All,
Cannot figure out why this would be causing the classic "Too many SQL Queries" error. As far as I know I have implemented what is best-practice for bulk processing.
However when insert tasks using data loader, it's generating the error above.
Any help pointers appreciated - thanks
trigger CloseOriginalTask on Task (after insert) { List<Task> taskRecords = [select TaskIdFollowedUp__c from Task where Id IN:Trigger.newMap.keySet()]; List<Task> tasksForUpdate = new List<Task>{}; if (taskRecords.size()!=0){ for (Task taskRecord : taskRecords){ Id taskId = taskRecord.TaskIdFollowedUp__c; List<Task> originalTask = [select Id, Status from Task where Id=:taskId]; for (Task t:originalTask){ t.Status = 'Completed'; tasksForUpdate.add(t); } } update tasksForUpdate; } }
You need to take the SOQL query to collect the "original" tasks out of the for-loop. Something like this...
All Answers
You need to take the SOQL query to collect the "original" tasks out of the for-loop. Something like this...
Hi Mike,
The exception off too many SQL Queries are being generated because you have placed a Query inside your for loop (marked in red)
for (Task taskRecord : taskRecords){
Id taskId = taskRecord.TaskIdFollowedUp__c;
List<Task> originalTask = [select Id, Status from Task where Id=:taskId];
for (Task t:originalTask){
t.Status = 'Completed';
tasksForUpdate.add(t);
}
}
Sorry for my slow reply. Been out of action for a few days.
Thanks both for quick responses.
That worked and increased my knowledge.
Cheers