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
Brian FordBrian Ford 

Too many SOQL queries but none in loops

I have a trigger on the Opportunity that creates a new Case when a custom opportunity field is marked True. At the bottom, I grab associated custom objects (Software_Key__c) from the opportunity and relate them to the new case.

When I test the trigger, line 59 is returning too many SOQL queries. No idea why because it isn't in a for loop. Anyone see what I'm missing?

trigger CreateCase on Opportunity (after update) {

    List<Opportunity> newOpps =;
    Map<Id, Opportunity> oldOpps = Trigger.oldMap;
    List<Case> caseList = new List<Case>();
    Map<Id, Id> ocrMap = new Map<Id, Id>();
    List<OpportunityContactRole> ocrList = [SELECT Id, ContactId, OpportunityId
                                            FROM OpportunityContactRole
                                            WHERE OpportunityId IN :newOpps AND IsPrimary = true];
    for (OpportunityContactRole ocr : ocrList)
    	ocrMap.put((Id) ocr.OpportunityId, (Id) ocr.ContactId);    
	for (Opportunity opp : newOpps)
		Opportunity beforeUpdate = oldOpps.get(opp.Id);

	   if (!beforeUpdate.Health_Check_Trigger__c && opp.Health_Check_Trigger__c && opp.New_Renew__c == 'New')
		Case thisCase = new Case();
		thisCase.AccountId = opp.AccountId;
		thisCase.Subject = '14-Day Health Check';
            	thisCase.Health_Check__c = true;
            	thisCase.OpportunityId__c = opp.Id;
            	thisCase.OwnerId = '00Ga00000045f3pEAA';
            	thisCase.RecordTypeId = '012a0000001NcLLAA0';
	            thisCase.ContactId = ocrMap.get(opp.Id);
	        catch(Exception e){}

    if (!caseList.isEmpty())
        	insert caseList;
        catch (Exception e){}   

    Map<Id, Id> caseMap = new Map<Id, Id>();
    for (Case c : caseList)
        caseMap.put((Id) c.OpportunityId__c, (Id) c.Id);
    List<Software_Key__c> skList = [SELECT Id, CaseId__c, Opportunity__c 
                                    FROM Software_Key__c 
                                    WHERE Opportunity__c IN :caseMap.keySet()];

    List<Software_Key__c> skToUpdate = new List<Software_Key__c>();
    for (Software_Key__c sk : skList)
        sk.caseId__c = caseMap.get(sk.Opportunity__c);
        update skToUpdate;
    catch (Exception e){}



As per your code mentioned ablove, line#59 is empty. Could you please check and mention the correct line.

Shyam BhundiaShyam Bhundia
it could be another trigger "using up" the soql queries.    can you confirm if there any other triggers on the opportunity object?  do yo have a case trigger?  As the insert on 45 will call any case triggers. 
David "w00t!" LiuDavid "w00t!" Liu
Shyam is right!

Governor limits apply to all triggers that are run as a result of a single save in Salesforce!

So let's say you have a trigger on Accounts that updates all the Contacts of the account.

All limits are calculated cumulatively across all Account triggers and also all Contact triggers when a single Account is saved!