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
sfadm sfadmsfadm sfadm 

Anonymous window Too many SOQL queries: 101

In order to achieve mass update of my records I tried to execute the following script:
List<Task> taskList = [SELECT status, CalculatedWorkingHours__c FROM Task WHERE CalculatedWorkingHours__c = -1.0 AND status = 'Completed'];
        for(Task task :taskList) {
            task.CalculatedWorkingHours__c = 1.0;
        }   
        update taskList;
        System.debug('taskList ' + taskList);

in Anonymous window in my Developer Console however I received the following message: 
`System.LimitException: Too many SOQL queries: 101`

Please advise how to avoid such exception and execute my script successfully?
Steven NsubugaSteven Nsubuga
The issue is not with the query here, but most likely with a trigger on the Task object. Check that the update trigger on the Task object does not have a SOQL query within a for loop.
Raj VakatiRaj Vakati
Try this code
 
List<Task> taskList = [SELECT status, CalculatedWorkingHours__c FROM Task WHERE CalculatedWorkingHours__c = -1.0 AND status = 'Completed' Limit 9999];
        for(Task task :taskList) {
            task.CalculatedWorkingHours__c = 1.0;
        }   
        update taskList;
        System.debug('taskList ' + taskList);

 
Sai harsha 7Sai harsha 7
Hi sfadm 

 I have seen your code , you made a mistake in updating the query list variable. 

create a one more list variable and add the query list variable to the newly created list var and update that variable.

try this code. 
 
List<Task> taskList = [SELECT status, CalculatedWorkingHours__c FROM Task WHERE CalculatedWorkingHours__c = -1.0 AND status = 'Completed'];

list<task> tsk1 = new list<task>();
       
 for(Task task :taskList) 
{
          task.CalculatedWorkingHours__c = 1.0;

         tsk1.add(task);
  }   
        update tsk1;

        System.debug('taskList ' + tsk1);

please mark it as best answer