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
krishnagkrishnag 

too many SOQL queries 101

hi i have a Asynchronous class with future method when i load data using dataloader.I am geting an exception saying

 

Apex script unhandled exception by user/organization: 005800000036bA4/00D80000000cSrC

Failed to invoke future method 'public static void updateRelationMethod(LIST<String>)' on class 'updateRelation' for job id '70780000003Dn62'

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

Class.updateRelation.updateRelationMethod: line 66, column 38
External entry point
public class updateRelation {

@future
public static void updateRelationMethod(list<String> AccountIds){

List<Account> acc = [select id,Company_Relationship__c,Direct_Markets_Customer__c,Z_Programs_Customer__c  from Account where Company_Relationship__c != 'Broker' and Company_Relationship__c !='Employee' and Id IN :AccountIds];

 for(Account temp :acc)
{
    System.debug('In if loop');


    list<Policy__c> NonActivePolicy1 = [select Id,Policy_Status__c from Policy__c where 
                                                     Company_Name__c =:temp.Id limit 1];
  // start of condition 2
  if(!NonActivePolicy1.isEmpty()){
      temp.Company_Relationship__c = 'Customer';
  list<Policy__c> ActivePolicy = [select Id,Policy_Status__c from Policy__c where Policy_Status__c ='Active'
                                                     and Company_Name__c =:temp.Id limit 1];
     //Start of condition 3
    if(!ActivePolicy.isEmpty()){
          temp.Relationship_Type__c = 'Current'; 
  
  }
  //END of if 3  
  
  //Start of condition4 1
  else{
        Date tempd = System.today()+180;
      list<Policy__c> NonActivePolicy = [select Id,Policy_Status__c,Policy_Expiration_Date__c from Policy__c where 
                                                Company_Name__c =:temp.Id 
                                                          and Policy_Expiration_Date__c >:tempd limit 1];
     if(!NonActivePolicy.isEmpty()){
        temp.Relationship_Type__c = 'Current';
        }
        else{
          list<Submission__c> subBondType =  [SELECT Id,Submission_Status__c,Policy_Status__c FROM Submission__c where 
                                                Account_Name__c =:temp.Id and Submission_Status__c ='Bound' limit 1]; 
            //start of condition7 5
            if(!subBondType.isEmpty()){
                 temp.Company_Relationship__c = 'Customer';
                 if(subBondType[0].Policy_Status__c == 'Active')
                 {
                     temp.Relationship_Type__c = 'Current';
                     }
                 else
                 {
                     temp.Relationship_Type__c = 'Former';   
               }}
               else{
               temp.Relationship_Type__c ='Former';
               }
            //end of if 5
         // Start of condition8 3
        
          
        
        
  }    }
//End of else 1
}
//end of if 2

//Start of condition5 2
else{
      list<Submission__c> subType =  [SELECT Id,Submission_Status__c,Policy_Status__c FROM Submission__c where 
                                               Account_Name__c =:temp.Id  limit 1]; 
 //start of condition6 4
 if(!subType.isEmpty()){
      list<Submission__c> subBondType =  [SELECT Id,Submission_Status__c,Policy_Status__c FROM Submission__c where 
                                                Account_Name__c =:temp.Id and Submission_Status__c ='Bound' limit 1]; 
            //start of condition7 5
            if(!subBondType.isEmpty()){
                 temp.Company_Relationship__c = 'Customer';
                 if(subBondType[0].Policy_Status__c == 'Active')
                     temp.Relationship_Type__c = 'Current';
                 else
                     temp.Relationship_Type__c = 'Former';   
               }
            //end of if 5
         // Start of condition8 3
         else {
            temp.Company_Relationship__c = 'Prospect';
            temp.Relationship_Type__c = 'Activity';
         } 
         //END of else 3    
 }
 //end of if 4
// START of condition9 4
else {
    System.debug('In last if loop');
      if(temp.Direct_Markets_Customer__c==True || temp.Z_Programs_Customer__c ==True)
      {
      temp.Company_Relationship__c = 'Programs';
      }
      else{
            temp.Company_Relationship__c = 'Prospect';
            temp.Relationship_Type__c = 'No Activity';    
          }
  }
// End of else 4
}
//END of else 2
  }
  //END of if 1
  ProcessorControl.inFutureContext = true;
update acc;
    }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
krishnagkrishnag

may be this causes because i put the queries in for loop I took them out of the loop and put them outside need to say what goes on now.

All Answers

krishnagkrishnag

may be this causes because i put the queries in for loop I took them out of the loop and put them outside need to say what goes on now.

This was selected as the best answer
krishnagkrishnag

success got rid of that error.It worked after taking the queries from for loop.