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
gb.apexgb.apex 

caused by: System.LimitException: Too many SOQL queries: 201

Hi All,
its too urgent, am written a batch class for populate the approval comment field in order object. now i am facing this error can u any one please help me to avoid governerlimits..thank alot in advance

global void execute(Database.BatchableContext BC,List<Order_vod__c> scope)
  {
      List<Order_vod__c> updOrdlist = new List<Order_vod__c>();
      //for(Order_vod__c order : scope)
      //{                      
      //   updOrdlist =  createOrdCmnts(scope);
      //}  
      for(Order_vod__c o2:scope)
      {
         ProcessInstance [] op = [SELECT Id,Status,targetobjectid,(SELECT Id,Comments FROM Steps) FROM ProcessInstance where targetobjectid = : o2.id limit 1];
        
         system.debug('1--------------Order ID -------------->'+ o2.id);
         system.debug('2-------------Process Instance -------------->'+ op);
        
         for (ProcessInstance op1 : op)
         {
            for (ProcessInstanceStep pis : op1.Steps)
            {
               system.debug('4-------------Process Instance Steps-------------->'+ pis);
               if(op1.Status == 'Approved')
               {
                   o2.PFE_IT_Approval_Comment__c = pis.Comments;
                   system.debug('3-------------o2.PFE_IT_Approval_Comment__c-------------->'+ o2.PFE_IT_Approval_Comment__c);
               }
            }
         }
         updOrdlist.add(o2);
      }
      system.debug('5-------------updOrdlist-------------->'+ updOrdlist);
      update updOrdlist;
    }
Sonam_SFDCSonam_SFDC
Please move the query outside the for loop - 

for(Order_vod__c o2:scope)
      {
         ProcessInstance [] op = [SELECT Id,Status,targetobjectid,(SELECT Id,Comments FROM Steps) FROM ProcessInstance where targetobjectid = : o2.id limit 1];

try something similar to this:
Set<ID> setscopeIDs = new Set<ID>();
        for(Order_vod__c sc: scope)
                {
                        setscopeIDs.add(sc.Id);
                }

for(Order_vod__c O2 : [SELECT Id,Status,targetobjectid,(SELECT Id,Comments FROM Steps) FROM ProcessInstance where targetobjectid in =: setscopeIDs ])
{FURTHER PROCESSING}
Swati GSwati G
Hi,

It is not a good practice to write query inside for loop. Instead of this you can use collection and query one time for all the orders.

Here is the code modified code:

global void execute(Database.BatchableContext BC,List<Order_vod__c> scope)
  {
      List<Order_vod__c> updOrdlist = new List<Order_vod__c>();
   Set<Id> orderIds = new Set<Id>();
      for(Order_vod__c order : scope)
      {                     
         orderIds.add(order.id);
   } 
  
   ProcessInstance [] op = [SELECT Id,Status,targetobjectid,(SELECT Id,Comments FROM Steps) FROM ProcessInstance where targetobjectid in: orderIds];
   Map<Id,ProcessInstance> mapOfOrderProcessInstance = new Map<Id, ProcessInstance>();
   for(ProcessInstance p: op){
  mapOfOrderProcessInstance.put(p.targetObjectId, p);
   }
  
      for(Order_vod__c o2:scope)
      {
        
       
         system.debug('1--------------Order ID -------------->'+ o2.id);
   
   if(mapOfOrderProcessInstance.containsKey(o2.Id)){
   ProcessInstance op1 = mapOfOrderProcessInstance.get(o2.Id);
   system.debug('2-------------Process Instance -------------->'+ op1);
            for (ProcessInstanceStep pis : op1.Steps)
            {
               system.debug('4-------------Process Instance Steps-------------->'+ pis);
               if(op1.Status == 'Approved')
               {
                   o2.PFE_IT_Approval_Comment__c = pis.Comments;
                   system.debug('3-------------o2.PFE_IT_Approval_Comment__c-------------->'+ o2.PFE_IT_Approval_Comment__c);
               }
            }
         }
         updOrdlist.add(o2);
      }
      system.debug('5-------------updOrdlist-------------->'+ updOrdlist);
      update updOrdlist;
    }
gb.apexgb.apex
Hi swathi and sonam for quick responce. i used swathi code its saved without error l,when i executing thet time am facing same error..thank you alot for ur responce...give me any suggestion to resolve it...
Swati GSwati G
Hi, 

Please share what error you are getting.
gb.apexgb.apex
Too many SOQL queries: 201
gb.apexgb.apex
3000 batches are there first 10 batches are successfully updating later we are facing this issue
Swati GSwati G
In the above code, we moved query outside for loop. so this issue should not come. Please see debug log whether this issue is related to batch or some triggers which are getting execute through the dml operationo Order_vod__c. Check triggers written in Order_vod__c whether you have any query inside for loop.