You need to sign in to do that
Don't have an account?
System.Exception: Too many SOQL queries: 101
Hello,
We have a class and trigger in place which is now producing the error listed below. It is referencing an Apex Class and an Apex Trigger of where the issue is stemming from.
Can someone help me figure out how I can correct this?
Thank you!
Line 47 of the Class.LeadTrigger can be found in this class code below:
for(User u : [Select Id, UserType, Contact.AccountId, Contact.Account.Member__c, Exclude_from_Zip_Placement__c From User Where isActive = true]){
This is the ApexTrigger code named referenced in the apex error code:
Trigger.triggerLead: line 3, column 1
We have a class and trigger in place which is now producing the error listed below. It is referencing an Apex Class and an Apex Trigger of where the issue is stemming from.
Can someone help me figure out how I can correct this?
Thank you!
Apex script unhandled trigger exception by user/organization: 005A0000005XYZb/00DA0000033432M triggerUpdAssignedLead: execution of BeforeUpdate caused by: System.Exception: Too many SOQL queries: 101 Class.LeadTrigger.<init>: line 47, column 1 Trigger.triggerLead: line 3, column 1
Line 47 of the Class.LeadTrigger can be found in this class code below:
for(User u : [Select Id, UserType, Contact.AccountId, Contact.Account.Member__c, Exclude_from_Zip_Placement__c From User Where isActive = true]){
public without sharing class LeadTrigger { //private static boolean alreadyExecuted = false; Private Set<String> OpenLeadStatus = new Set<String>{'Awaiting Decision','Initiating Contact','New', 'Quoting', 'Re-Engaged', 'Job Take'}; Private String QuotingStatus = 'Quoting'; Private String QuotingCampaignNumber = '34099'; Private static String MODELER_LEAD = 'Modeler Lead'; Private static String HOW_LEAD = 'Show Lead'; Private static String STATUE = 'Statue'; public static String ClosedStatus = 'Closed By EF'; public static String ProjectDelayedStatus = 'Project Ending'; //public static boolean isTest {get; set;} //public static boolean isTestUpdateLead {get; set;} public static boolean isUpdateLeadStatus {get; set;} public static boolean isUpdateHistoryLog {get; set;} //New class variable creatd for optimization - 10/2016 //Prevent SOQL Query governor limit exception private Map<String,Id> recTypes = new Map<String,Id>(); private final string queueInsideSalesId; private final string dummyCampaignId = null; private Map<Id, User> activeUsers = new Map<Id, User>(); private Set<Id> excludedZipAssignUsers = new Set<Id>(); //Creating class constructor to initialize class objects - 11/2017 //Prevent SOQL Query governor limit exception public LeadTrigger(){ //Retrieving RecordType information for(Schema.RecordTypeInfo rt : Lead.SObjectType.getDescribe().getRecordTypeInfos()){ String rtName = rt.getName(); if((rtName == MODELER_LEAD)||(rtName == SHOW_LEAD)){ recTypes.put(rtName, rt.getRecordTypeId()); } } //Retrieving values for the Lead Assignment - AssignLeads() method //Retrieving the default owner (Inside Sales Queue) for(QueueSobject queue : [Select QueueId From QueueSobject Where SobjectType = 'Assigned_Lead__c' and Queue.Name = 'Inside Sales']){ queueInsideSalesId = queue.QueueId; } //Retrieving Dummy Campaign ID for Assigning New Leads to a Campaign for(Campaign c : [Select Id, Campaign_Number__c From Campaign Where Name Like '%Dummy%' Limit 1]){ dummyCampaignId = c.Id; } // Retrieving Active Users List for Lead Assignment for(User u : [Select Id, UserType, Contact.AccountId, Contact.Account.Member__c, Exclude_from_Zip_Placement__c From User Where isActive = true]){ //Retrieving Users excluded by Zip Assignment if(u.Exclude_from_Zip_Placement__c == true){ excludedZipPlaceUsers.add(u.Id); }else{ activeUsers.put(u.Id, u); } } }
This is the ApexTrigger code named referenced in the apex error code:
Trigger.triggerLead: line 3, column 1
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); // 10/22/2015 Ambassador enhancement Tia Xuan if (Trigger.isBefore && Trigger.isUpdate){ lt.CreateLastVisitTask(Trigger.new); lt.CopyHistoryLog(Trigger.new, Trigger.oldMap); } }
Greetings to you!
The following 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).
System.LimitException: Too many SOQL queries: 101 error
-Avoid SOQL queries that are inside FOR loops.
Try this code from line 47 in Class:
List<User> userList= [Select Id, UserType, Contact.AccountId, Contact.Account.Member__c, Exclude_from_Zip_Placement__c From User Where isActive = true];
for(User u : userList){
//Retrieving Users excluded by Zip Assignment
if(u.Exclude_from_Zip_Placement__c == true){
excludedZipPlaceUsers.add(u.Id);
}else{
activeUsers.put(u.Id, u);
}
}
Notes:
All the SOQL queries in triggers fired from one call or context will be counted against the limit of 100.
Salesforce cannot disable or raise the Governors Limit.
Resolution
Resolve the "Too many SOQL queries: 101" error
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.
-Avoid SOQL queries that are inside FOR loops.
-Follow the key coding principals for Apex Code in our Developer's Guide.
-Review our best practices for Trigger and Bulk requests:
-Best practices for Triggers and Bulk requests (Force.com Apex Code Developer's Guide)
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha
All Answers
Select Id, UserType, Contact.AccountId, Contact.Account.Member__c, Exclude_from_Zip_Placement__c From User Where isActive =true
The number of SOQL queries that can be issued in Salesforce are 100 and it will throw an error if it exceeds the same.
//Retrieving Users excluded by Zip Assignment
for(User us : u){
if(us.Exclude_from_Zip_Placement__c == true){
excludedZipPlaceUsers.add(us.Id);
}
else{
activeUsers.put(us.Id, us);
}
}
}
try this and let me know if it works.
I receieved this error:
This is line 61 and Line 62
Greetings to you!
The following 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).
System.LimitException: Too many SOQL queries: 101 error
-Avoid SOQL queries that are inside FOR loops.
Try this code from line 47 in Class:
List<User> userList= [Select Id, UserType, Contact.AccountId, Contact.Account.Member__c, Exclude_from_Zip_Placement__c From User Where isActive = true];
for(User u : userList){
//Retrieving Users excluded by Zip Assignment
if(u.Exclude_from_Zip_Placement__c == true){
excludedZipPlaceUsers.add(u.Id);
}else{
activeUsers.put(u.Id, u);
}
}
Notes:
All the SOQL queries in triggers fired from one call or context will be counted against the limit of 100.
Salesforce cannot disable or raise the Governors Limit.
Resolution
Resolve the "Too many SOQL queries: 101" error
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.
-Avoid SOQL queries that are inside FOR loops.
-Follow the key coding principals for Apex Code in our Developer's Guide.
-Review our best practices for Trigger and Bulk requests:
-Best practices for Triggers and Bulk requests (Force.com Apex Code Developer's Guide)
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks and Regards,
Deepali Kulshrestha