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
Adam MarksAdam Marks 

Too many SOQL queries: 101

Have another Trigger that is erroring out. Again, this is above my area of expertise. I can't see anything jumping out at me that would cause this to error , but I amm sure it's something simple. Also am I correct that this Trigger is simply updating the License lookup field on existing cases?

Trigger updateCasesByAccount on License__c (before update, before insert) {

  // To be SF Limit compliant we limit our sql calls by keeping as much as possible outside the loop
  Set< Id > licenseAccountIds = new Set< Id >();
  
  // Add each licenses related account id to the licenseAccountIds set
  // Also add each license to the licenses list
  Integer countC = 0;
  for (License__c lic:Trigger.new) {
    licenseAccountIds.add(lic.Account__c);
    countC  ++;
  }
  // Debug
  //trigger.new[0].Ship_Address1__c = 'Trigger found: ' + countC;
  
  // Get all cases related to the licenses based on account it
  List<Case> cases = [SELECT id, accountId, FPB_License__c, CaseNumber FROM Case WHERE accountId in: licenseAccountIds]; 
  
  // Create map of account id (key) to license id (value)
  Integer countM = 0;
  Map<Id,Id> oppmap = new Map<Id,Id>();
 for (License__c lic:Trigger.new) {
    oppmap.put(lic.Account__c, lic.Id);
    countM ++;
  }
  // Debug
  //trigger.new[0].Ship_City__c = 'Mapped: ' + countM;
  
  // Loop through csses and match to licenses using account id
  Integer countCA = 0;
  for (Case cas:cases) {
      // Use map ( basically hash table) to avoid more loops
        cas.FPB_License__c = oppmap.get(cas.accountId);
        
        countCA ++;
  }
  update cases;
  // Debug
  //trigger.new[0].Ship_City__c = 'Related Cases: ' + countCA;

}

Chris CalhounChris Calhoun
Here's a real quick suggestion. Line 34 add
if (cases.size() = 100);
{
  update cases;
  cases.clear();
}


Shivanath DevnarayananShivanath Devnarayanan
Hey Adam,

By the looks of this trigger, it doesnt seem the issue is here...
  • for the too many SOQL queries to be popping up, there must be 100 SOQL query lines issued ,
  • Which usually happens if SOQL is inside a for Loop.
Check the following 
  • Are there other triggers on cases ? 
  • have you installed some unmanaged package that has a case trigger? 
  • Possibly, in one of the related triggers you might be having a code that is complex enough to run so many SOQL
here's how you can find it 
  • have the developer console open when you run this code, check the dev console log to see the exact class and line number of where it is failing 
here --> https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_debugging_system_log_console.htm

 
 Hope that helps !