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
MikeGillMikeGill 

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;
	
	}

}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
davescardavescar

You need to take the SOQL query to collect the "original" tasks out of the for-loop.  Something like this...

 

 

trigger CloseOriginalTask on Task (after insert) {
	
	List<Task> originalTasks = new List<Task>();
	Set<Id> origTaskIdSet = new Set<Id>();
	
	for (Task task : Trigger.new) {
		origTaskIdSet.put(task.TaskIdFollowedUp__c);
	}
	
	originalTasks = [SELECT Id, Status FROM Task WHERE Id in :origTaskIdSet];
	
	for ( Task task : originalTasks) {
		task.Status = 'Completed';
	}
	
	update originalTasks;
}

 

 

All Answers

davescardavescar

You need to take the SOQL query to collect the "original" tasks out of the for-loop.  Something like this...

 

 

trigger CloseOriginalTask on Task (after insert) {
	
	List<Task> originalTasks = new List<Task>();
	Set<Id> origTaskIdSet = new Set<Id>();
	
	for (Task task : Trigger.new) {
		origTaskIdSet.put(task.TaskIdFollowedUp__c);
	}
	
	originalTasks = [SELECT Id, Status FROM Task WHERE Id in :origTaskIdSet];
	
	for ( Task task : originalTasks) {
		task.Status = 'Completed';
	}
	
	update originalTasks;
}

 

 

This was selected as the best answer
ipsita.biswas@in.v2solutions.comipsita.biswas@in.v2solutions.com

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);    
            }
        
            
    }

MikeGillMikeGill

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