+ Start a Discussion
Surinder Singh 25Surinder Singh 25 

Too many soql - Have not used any soql inside loop

/*
 *  Purpose: Defaults Business segment as per Custom settings data and populates this field.
 *           Products__c = "Support Service Requested"
 *           Calculates SLA Lapse Days on Opportunity
 *           Calculate response Time on Opportunity
 *           
 *  @Author: Pranjal Singh           
 **/
public class DFCS_defaultBusinessSegment
{
  public static Integer i =0;
  //public static Integer RECORD_COUNT = [SELECT count() FROM BusinessSegment__c];
  public static long hours;
  public static Integer value;
  public static ID  BussId  = [select Id from BusinessHours where isdefault = true].ID;

 /*   public static void defaultBusinessSegment(List<Opportunity> oppList)
    {
       
        for(Opportunity opp: oppList){
            while(i != RECORD_COUNT){
                i++;
                BusinessSegment__c obj = BusinessSegment__c.getValues('BS-'+i); //Support Service
               if((opp.Product_Type__c == obj.Product_Family__c) &&
                    (opp.Products__c == obj.Support_Service__c))
                {   
                   opp.BusinessSegment__c = obj.Business_Segment__c;
              }
            }
        }
    }
  
  */
  public static void SLA_Lapse_Days(List<Opportunity> oppList) 
  {
    Date CurDate= system.Today(); 
   // if(bh == null) bh = [select Id from BusinessHours where isdefault = true ];
    for(Opportunity opp: oppList)
     {
        if(opp.Opportunity_SLA_Commitment_Date__c!=Null)
           {
           
             if((opp.Release_Date__c!=Null)&&(opp.Release_Date__c>opp.Opportunity_SLA_Commitment_Date__c))
              {
           
              hours = Math.abs(BusinessHours.diff(BussId,opp.Release_Date__c,opp.Opportunity_SLA_Commitment_Date__c)/1000/60/60); 
              value = hours.intValue();
              value= value/9;
              opp.SLA_Lapase_Days__c=value;
              }
              
              if((opp.Release_Date__c==Null)&&(opp.Opportunity_SLA_Commitment_Date__c<=CurDate))
              {
              
              hours = Math.abs(BusinessHours.diff(BussId,opp.Opportunity_SLA_Commitment_Date__c,CurDate)/1000/60/60); 
              value = hours.intValue();
              value= value/9;
              opp.SLA_Lapase_Days__c=value;
              }
              
             if((opp.Release_Date__c!=Null)&&(opp.Release_Date__c<opp.Opportunity_SLA_Commitment_Date__c)||(opp.Release_Date__c==Null)&&(opp.Opportunity_SLA_Commitment_Date__c>=CurDate))
              {
              opp.SLA_Lapase_Days__c=0;
              }
              
              } 
             
           }
     
     }
  
  
  
  
  
  

}
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi, Surinder,

Resolve the "Too many SOQL queries: 101" error
To fix the issue, change your code so that the number of SOQL fired is less than 100.
If you need to change the context, you can use @future annotation which will run the code asynchronously.
 
Best practices to avoid exceeding the Governors Limit
Since Apex runs on a multi-tenant platform, the Apex runtime engine strictly enforces limits to ensure code doesn't monopolize shared resources.
 
Avoid SOQL queries that are inside FOR loops.
Follow the key coding principals for Apex Code in our Developer's Guide.
Review our best practices for Trigger and Bulk requests:
Best practices for Triggers and Bulk requests (Force.com Apex Code Developer's Guide)
Best practices for Triggers (Salesforce Developer Blog)
Please refer the below link for reference. Hope it will be helpful.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar