title: FATAL_ERROR|System.LimitException: Too many SOQL queries: 201
body: <p>I am getting too many SOQL queries 201 error in apex class</p>
  
  <p>I tried to check the no of queries within loop 
  Below is exact error - </p>
  
  <blockquote>
    <p>11:48:43.9 (2785518121)|FATAL_ERROR|System.LimitException: Too many SOQL queries: 201</p>
  </blockquote>
  
  <pre><code>Class.GEN_CalculateActToWinScores.calcUserEligible: line 1343, column 1
  Class.GEN_ActonFactsScoreUserEligibleBatch.execute: line 74, column 1
  </code></pre>
  
  <blockquote>
    <p>11:48:43.9 (2851114444)|CODE_UNIT_FINISHED|GEN_ActonFactsScoreUserEligibleBatch
    11:48:43.9 (2852614277)|EXECUTION_FINISHED</p>
  </blockquote>
  
  <p>Below is the code for method <code>GEN_CalculateActToWinScores.calcUserEligible</code> - </p>
  
  <pre><code>   // Method for set user as ready for AoF
     public static void calcUserEligible(List&lt;User&gt; usersList ){
  
              List&lt;Act_on_Facts__c&gt; actOnFactDelete = new List&lt;Act_on_Facts__c&gt;();
              Set&lt;String&gt; oppOpenStageNameSet = new Set&lt;String&gt;();
              oppOpenStageNameSet.add(GEN_Constants.OPPORTUNITY_IDENTIFY);
              oppOpenStageNameSet.add(GEN_Constants.OPPORTUNITY_QUALIFY);
              oppOpenStageNameSet.add(GEN_Constants.OPPORTUNITY_PROPOSE);
              oppOpenStageNameSet.add(GEN_Constants.OPPORTUNITY_NEGOTIATE);
  
              Set&lt;String&gt; oppReadOnlyRecordTypeNameSet = new Set&lt;String&gt;();
              oppReadOnlyRecordTypeNameSet.add(GEN_Constants.READONLY_SINGLE_ACCOUNT_OPP_RECORDTYPENAME);
              oppReadOnlyRecordTypeNameSet.add(GEN_Constants.READONLY_WON_AND_DONE_OPP_RECORDTYPENAME);
              oppReadOnlyRecordTypeNameSet.add(GEN_Constants.READONLY_CHILD_OPP_RECORDTYPENAME);
              oppReadOnlyRecordTypeNameSet.add(GEN_Constants.READONLY_MULTI_ACCOUNT_OPP_RECORDTYPENAME);  
  
              if(usersList.size() &gt; 0){
  
                  List&lt;Id&gt; idList = new List&lt;Id&gt;();
                  Integer countResult = 0;            
                  List&lt;User&gt; newUserList = new List&lt;User&gt;();                
  
                  Integer limitQuery;     //10000 //2001
                  Integer limitResult;    //2000
  
                  if(CSL_ActOnFactsLimits__c.getValues('QUERY_LIMIT') != null){
                      limitQuery = Integer.valueOf(CSL_ActOnFactsLimits__c.getValues('QUERY_LIMIT').Value__c);
                  }
                  if(CSL_ActOnFactsLimits__c.getValues('MAX_QUERY_RESULTS') != null){
                      limitResult = Integer.valueOf(CSL_ActOnFactsLimits__c.getValues('MAX_QUERY_RESULTS').Value__c);  
                  }   
  
  
                  Boolean eligible = true;
  
                  for (User userElement : usersList){
  
                      String logDetail = ' QUERY_LIMIT:limitQuery: '+limitQuery+' MAX_QUERY_RESULTS:limitResult: '+limitResult;
  
                      logDetail += ' userElement.id: '+userElement.id;
  
                      eligible = true;
  
                      CSH_ActOnFacts_UserEligible__c userEligible = null;
                      if(CSH_ActOnFacts_UserEligible__c.getValues(userElement.id) != null){
                          userEligible = CSH_ActOnFacts_UserEligible__c.getValues(userElement.id);
                      }
  
                      CSH_ActOnFacts_UserEligible__c profileEligible = null;
                      if(CSH_ActOnFacts_UserEligible__c.getValues(userElement.ProfileId) != null){
                           profileEligible = CSH_ActOnFacts_UserEligible__c.getValues(userElement.ProfileId);
                      }
  
                      List&lt;Act_on_Facts__c&gt; actonfacts = [select id from Act_on_Facts__c where Lookup_User__c = : userElement.id limit 1];
  
                      if(userElement.ManagerId == null){
                          userElement.Line_Manager_Optional__c = true;
                          userElement.Act_On_Facts_Manager_List__c = null;
                      }
  
                      if(userElement.Default_Macro_Segment__c == null){
                          userElement.Default_Macro_Segment__c = 'None';
                      }
  
                      if (userElement.IsActive == false){
                          eligible = false;
                      }else if(profileEligible != null || userEligible != null) {   
                           eligible = false;                      
                      }else{
  
                          countResult = [select count() from Account WHERE OwnerId = :userElement.id LIMIT :limitQuery];
                          logDetail += ' Account.countResult: '+countResult;
  
                          if(countResult!=null &amp;&amp; countResult &gt; limitResult ){
                              eligible = false;
                          }
  
                          if(eligible){   
  
                            if(oppOpenStageNameSet !=null &amp;&amp; oppOpenStageNameSet.size()&gt;0 &amp;&amp; oppReadOnlyRecordTypeNameSet !=null &amp;&amp; oppReadOnlyRecordTypeNameSet.size()&gt;0){             
  
                              countResult = [select count() from Opportunity WHERE OwnerId = :userElement.id AND StageName IN:oppOpenStageNameSet AND RecordType.DeveloperName NOT IN: oppReadOnlyRecordTypeNameSet LIMIT :limitQuery];
                              logDetail += ' Opportunity.countResult: '+countResult;
  
                              if(countResult!=null &amp;&amp; countResult &gt; limitResult ){
                                eligible = false;
                              }
                            }                              
                          }                   
                      }
  
                      if(!eligible){
  
                          userElement.Act_on_Facts_Eligible__c = false;
  
                          // remove user from Act on Facts
                          if(actonfacts != null &amp;&amp; actonfacts.size()&gt;0){
                              for(Act_on_Facts__c a : actonfacts){
                                  actOnFactDelete.add(a);
                              }
                          }
  
                      }else{
                          userElement.Act_on_Facts_Eligible__c = true;
                      }      
  
                      logDetail += ' userElement.Act_on_Facts_Eligible__c: '+userElement.Act_on_Facts_Eligible__c;
  
                      ApplicationLog.logEntry(ApplicationLog.SEVERITY_INFO, 'A2WBatch', 'GEN_ActonFactsScoreUserEligibleBatch:'+userElement.id+': ', logDetail);
  
                      newUserList.add(userElement);
                  }
  
                  try{
                      if(newUserList.size()&gt;0){
                          update newUserList;   
                          system.debug('HC Update- newUserList ' + newUserList);                      
                      }
  
                      if(actOnFactDelete.size()&gt;0){
                          delete actOnFactDelete;
                          system.debug('HC Update- actOnFactDelete ' + actOnFactDelete);
                      }
                  }catch (Exception e){
                      system.debug('Error updating user ' + e);
                  }  
              }
     } 
  </code></pre>
  
  <hr>
  
  <p>Code for <code>GEN_ActonFactsScoreUserEligibleBatch.execute</code>- </p>
  
  <pre><code>    global void execute(Database.BatchableContext BC, List&lt;sObject&gt; scope){
           CSH_A2W_Settings__c a2wCS = CSH_A2W_Settings__c.getInstance();         
          if(scope != null){ 
  
              List&lt;User&gt; userList = scope;
  
              if(userList.size()&gt;0){
  
                  if(CSL_ActOnFactsLimits__c.getValues('run_userTrigger') != null){
                      CSL_ActOnFactsLimits__c run_userTrigger = CSL_ActOnFactsLimits__c.getValues('run_userTrigger');
                      run_userTrigger.Value__c = 'false';
                      update run_userTrigger; 
                  }
  
                  //GEN_CalculateActOnFactsScores.calcUserEligible(userList);     //Commented as part of Decommission activity of AoF
                  if(a2wCS != null &amp;&amp; a2wCS.Enabled_in_Batches__c == True){
                      GEN_CalculateActToWinScores.calcUserEligible(userList);
                  }
              } 
          } 
      }
  </code></pre>
  
  <p>I am trying to analysis what will be the best possible ways to remove these errors or is there any alternate way to implement the same.</p>