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
mandycmandyc 

System.LimitException: Too many SOQL queries: 101

Hi All,

 

I'm getting the following error: System.LimitException: Too many SOQL queries: 101. I didn't think my query was inside a loop so I'm unsure how to fix the problem. Your help is greatly appreciated!

 

trigger setContactAndAccountIdOnOrder on Lead (after update) {

    Map<String,Id> Ids = new Map<String,Id>(); 

    for(Lead lead : Trigger.new) {
        if (lead.IsConverted) {
            Ids.put(lead.Person_Id__c,lead.Id);
        }
    } 
    
    List<Order__c> orders = [select Id, Person_Id__c, Contact__c, Account__c from Order__c WHERE Person_Id__c IN :Ids.keySet()];
        for (Order__c o : orders) { 
            Id personId = Ids.get(o.Person_Id__c);
            Lead ld = trigger.newMap.get(personId);
            o.Contact__c = ld.ConvertedContactId;
            o.Account__c = ld.ConvertedAccountId;
        }
        update orders;        
}

 

colemabcolemab

Do you have other triggers that are beng fired from this trigger?  Per haps a update order trigger?  And maybe that/those triggers are causing the query limit to be hit?

mandycmandyc

There is an after update trigger on the Order__c object. It does contain one query that is within a for loop....so, it looks like this is the problem. I need the ports map to be defined outside the if statement because I reference the size of ports later. Any suggestions on how to rework this code is appreciated!

 

    // Map of Portfolios matching the groups set above
    Map<String, Portfolio_Lookup__c> ports = new Map<String, Portfolio_Lookup__c>();
    if (groups.size() > 0){
        for (Portfolio_Lookup__c pl : [SELECT Name FROM Portfolio_Lookup__c WHERE IsDeleted = False AND Name IN :groups]){
            ports.put(pl.Name, pl);
        }
    }

 

Bhawani SharmaBhawani Sharma
1. Probably you are having multiple triggers on Lead for update event.
2. You have trigger Order__c
3. Triggers are firing recursively.
4. You have workflow field update, which is causing triggers to execute again.