+ Start a Discussion

System Exception: Too many SOQL queries: 101

I'm running into an issue with our LeadTrigger Apex Class. The error message received via email was: 

Apex script unhandled trigger exception by user/organization: 005A0000008YrGG/00DA0000000ISJM

triggerUpdAssignedLead: execution of BeforeUpdate

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

Class.LeadTrigger.PopulateAmbassador: line 438, column 1
Trigger.triggerLead: line 14, column 1

Starting at line 438 is the apex code causing the error below. 
Can someone help in finding a solution? I believe this may be caused by a FOR loop issue? 

I've also included the Apex Trigger code below this code, since it was referenced in the error message. 
List<Ambassador__c> ambs = [select id, Ambassador_ID__c, Ambassador_Name__c from Ambassador__c where Ambassador_ID__c =: ambassadorIds];
        Map<String, Ambassador__c> ambMap = new Map<String, Ambassador__c>();
        for (Ambassador__c amb : ambs)
            ambMap.put(amb.Ambassador_ID__c, amb);
        for (Lead ld : leadsToTreat) {
            if (ambMap.containsKey(ld.Ambassador_ID__c)) {
                ld.Ambassador_Name__c = ambMap.get(ld.Ambassador_ID__c).Ambassador_Name__c;

This is the Trigger.triggerLead Apex code which was referenced in the error message. Line 14 starts as: 

Trigger triggerLead on Lead (after insert, before insert, before update, after update) {

    LeadTrigger lt = new LeadTrigger();
    if (Trigger.isInsert)
        lt.LeadInsertion(Trigger.New, Trigger.isBefore, Trigger.isAfter);
    else if (Trigger.isBefore)
        lt.LeadUpdate(Trigger.New, Trigger.old, Trigger.isBefore);
    if (Trigger.isAfter) {
      lt.ReassignALOnClosedLeads(Trigger.New, Trigger.old, null);
      lt.AddToCampaign(Trigger.New, Trigger.oldMap);
      lt.ProcessQuotingLeads(Trigger.New, Trigger.old);
    } else 
      lt.PopulateAmbassador(Trigger.New, Trigger.old);   
    if (Trigger.isBefore && Trigger.isUpdate){
      lt.CopyHistoryLog(Trigger.new, Trigger.oldMap);   

Khan AnasKhan Anas (Salesforce Developers) 

Greetings to you!

This error appears when you exceed the Execution Governors Limit (you can run up to a total 100 SOQL queries in a single call or context): https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm

To fix the issue, change your code so that the number of SOQL fired is less than 100.
If you need to change the context, you can use @future annotation which will run the code asynchronously.
Best practices to avoid exceeding the Governors Limit:

Since Apex runs on a multi-tenant platform, the Apex runtime engine strictly enforces limits to ensure code doesn't monopolize shared resources.
Reference: https://help.salesforce.com/articleView?id=000181404&type=1

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas