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
Sarika PatelSarika Patel 

System.LimitException: Too many SOQL queries: 101 on Trigger

Hi,

I have created a custom object for "Contracts" and another custom object called "Contract Applicable Accounts". There is a rollup summary field on Contracts having total count of "Contract Applicable accounts" called Total_Applicable_Accounts__c.

Now I have created a trigger which on update of Contract object, checks all the accounts listed in "Contract Applicable Accounts" object and updates certain fields in an Account Object.

Here it is:

trigger UpdateAccount on Contract__c (before update) {
Contract__c[] Contract = Trigger.new;
try
{
    if(contract[0].Id != null)
    {
        if(contract[0].Total_Applicable_Accounts__c > 0)
        {
       
            List<Applicable_Accounts__c> ApplicableAccounts = [Select Id,Account_Name__c from Applicable_Accounts__c where Contract__c =: contract[0].Id ];
            System.debug(ApplicableAccounts.size());
            if(ApplicableAccounts != null && ApplicableAccounts.size() > 0)
            {
               for(Applicable_Accounts__c appaccount : ApplicableAccounts)
               {
                   if( appaccount.Account_Name__c != null)
                   {
                       Account objaccount = [ Select Name from Account where Id =: appaccount.Account_Name__c];                  
                       if(contract[0].Active__c != null)
                      {
                        objaccount.Active_Hidden__c = contract[0].Active__c;        
                        if(contract[0].Active__c == 'Yes')
                        {
                            if(contract[0].Id != null)
                            {  objaccount.Contract_Hidden__c = contract[0].Id; }    
                           if(contract[0].Contract_Details__c!=null)                       
                           { objaccount.Contract_Details_Hidden__c = contract[0].Contract_Details__c;}
                           if(contract[0].Special_Quoting_Instructions__c!=null)        
                           { objaccount.Special_Quoting_Instructions_Hidden__c = contract[0].Special_Quoting_Instructions__c;}
                           if(contract[0].Special_Report_Links__c!=null)
                           { objaccount.Special_Report_Links_Hidden__c = contract[0].Special_Report_Links__c;}
                            if(contract[0].Contract_Manager__c!=null)
                           { objaccount.Contract_Manager_Hidden__c = contract[0].Contract_Manager__c;  }
                           if(contract[0].Pricebook__c!=null)
                           { objaccount.Pricebook_Hidden__c = contract[0].Pricebook__c;  }                  
                        }
                        else
                        {
                             objaccount.Contract_Hidden__c = null;
                             objaccount.Contract_Manager_Hidden__c = null;
                             objaccount.Contract_Details_Hidden__c =null;
                             objaccount.Special_Quoting_Instructions_Hidden__c=null;
                             objaccount.Special_Report_Links_Hidden__c=null;
                             objaccount.Pricebook_Hidden__c = null;
                             
                        }       
                       update objaccount;
                    }  
               }  }
            }
                
        }
        List<Account> DeletedApplicableAccounts=[ Select Id,Name,Total_Applicable_Accounts_Hidden__c from Account where Contract_Hidden__c =:contract[0].Id and Total_Applicable_Accounts_Hidden__c=0 ];
         System.debug(DeletedApplicableAccounts.size());
          if(DeletedApplicableAccounts != null && DeletedApplicableAccounts.size() > 0)
            {
             for(Account deletedappaccount : DeletedApplicableAccounts)
               {
                if( deletedappaccount.Id != null && deletedappaccount.Total_Applicable_Accounts_Hidden__c == 0 )
                   {
                            deletedappaccount.Active_Hidden__c = 'No';
                            deletedappaccount.Contract_Hidden__c = null;
                            deletedappaccount.Contract_Manager_Hidden__c = null;
                            deletedappaccount.Contract_Details_Hidden__c =null;
                            deletedappaccount.Special_Quoting_Instructions_Hidden__c=null;
                            deletedappaccount.Special_Report_Links_Hidden__c=null;
                            deletedappaccount.Pricebook_Hidden__c = null;
                            
                            update deletedappaccount;
                   }
                }
            }
    }               
    
  
}
 catch(Exception ex)
    {
        System.debug('The following exception has occurred: ' + ex.getMessage());
        contract[0].addError('Exception has occured updating Account on update of Contract object');
    }   

}

The logic of this trigger is working fine, but during execution its giving me the error of "Too many SOQL queries". Could anyone please let me know where am I going wrong?

Thanks,

Sarika Patel

Sarika PatelSarika Patel
Its a bit urgent please help
Vivek DeshmaneVivek Deshmane
Hi Sarika,
Here is an example of how you can use a combination of System.debug statements and the Limits Apex class to generate some very useful output as it relates to governor limits and the overall efficiency of your code. Will help you to check from where it's near to limt.
    System.debug('Total Number of SOQL Queries allowed in this apex code context: ' +  Limits.getLimitQueries());
   Please refere following link.
https://developer.salesforce.com/page/Best_Practice:_Use_of_the_Limits_Apex_Methods_to_avoid_Hitting_Governor_Limits
Regards,
-Vivek