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
Sushmitha B 15Sushmitha B 15 

In the final method of batch class sending callout fails with SOQL Limit

I have a schedulable batch class which is stateful. In execute am inserting upto 1000 records.

In final method  i have a list of ids of inserted record

global void finish(Database.BatchableContext BC)
  {
                    if(Ids.size()>0)
                    {
                      for(string id : Ids)
                      {
                        // calling a static method sending id as paramter.
                      }
                    }
 
  }


In for loop for every id calling an static method with id as parameter.

The call static method will created an instance of another class which has nearly 10 queries and an http request.

am getting Too many SOQL queries: 201 

This is my static method

global  class SClass
{
  
  public static void SMethod(String id)
  {      
                         
       
         Sclass2 s = new sclass2 (Id,false);
         s.test();// this method has http request
   }           
        
}
 

why do i get this even when we have created new instance for every id. 

Any solution please.

Thanks,

 

Marcelo CostaMarcelo Costa

Hey Sushmita,
As the error states, you are hitting a governor limit. You can only call up to 200 SOQL queries in an asynchronous execution context and looks like you are hitting that one...

The issue for sure is the queries inside the for loop (which is a bad practice). 

The solution for that is to refactor the code to ensure it is batch friendly (Usually by querying anything you need to query outside of the loop).

The callout will be a challenge too, as you might hit an apex execution time limit due to a long execution time depending on the integration strategy you are using. Again, refactor to ensure that your code and your callout are batchable.
Good Luck