You need to sign in to do that
Don't have an account?
Mikhail Nitko 5
CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY: ProcessWebFields: System.LimitException: Too many SOQL queries: 101
Hi folks and thank you for taking a look at this question,
I am getting this error in a trigger in an org I inherited, but the error is not always presenting itself and I cant figure out exactly how to fix it.
All my research shows that it is a governor limit error, but I'm not sure how to fix it in this inherited code. And the code is as follows:
Thank you for helping folks.
I am getting this error in a trigger in an org I inherited, but the error is not always presenting itself and I cant figure out exactly how to fix it.
All my research shows that it is a governor limit error, but I'm not sure how to fix it in this inherited code. And the code is as follows:
trigger ProcessWebFields on Lead (after insert, after update) { for (Lead updatedLead : Trigger.new) { // If these criteria are met, we've just create a Lead and // there are values in the Web_UMOL field. if(updatedLead.Web_Program_of_Interest__c != NULL && updatedLead.Primary_Program_of_Interest__c == NULL){ // Break up the Web_ field String[] umolProgramsArr = updatedLead.Web_Program_of_Interest__c.split(';'); // Create Point of Interest records for(String program : umolProgramsArr) { Utilities.registerInterest(updatedLead.Id, program); } // Set the main Program of Interest List<Program__c> primaryProgramList = [SELECT Id FROM Program__c WHERE Name = :Utilities.getRealProgramName(umolProgramsArr[0])]; if(primaryProgramList.size() == 1){ Utilities.setPrimaryProgramOfInterest(updatedLead.Id, umolProgramsArr[0]); Utilities.checkPrimaryPOI(updatedLead.Id, primaryProgramList[0].Id); } // Otherwise, we are evaluating a change made that will effect the Primary // Program of Interest lookup field. } else { // If no Trigger.old, it's the first time running through and we don't want this code. if(Trigger.old != null){ Lead oldLead = Trigger.oldMap.get(updatedLead.Id); // Check to see if there is a change to the Primary_Program_of_Interest__c lookup if(updatedLead.Primary_Program_of_Interest__c != oldLead.Primary_Program_of_Interest__c){ Utilities.syncPrimaryPOI(updatedLead.Id, updatedLead.Primary_Program_of_Interest__c); } } } }
Thank you for helping folks.
Most of the time, when you see a SOQL request inside a loop, you could have a problem of "Too many SOQL queries: 101" if you update in bulk mode your data.
for (Lead updatedLead : Trigger.new) {
...
List<Program__c> primaryProgramList = [SELECT Id FROM Program__c
WHERE Name = :Utilities.getRealProgramName(umolProgramsArr[0])];
....
}
The common technique is to create a set of values of values used in the where clause (here mySet of names) inside the loop and you just use one SOQL query outside the loop with "WHERE Name in :mySet"
Set<String> myNames = new Set<String>();
for (Lead updatedLead : Trigger.new) {
String[] umolProgramsArr = updatedLead.Web_Program_of_Interest__c.split(';');
myNames.add( Utilities.getRealProgramName(umolProgramsArr[0]) );
}
List<Program__c> primaryProgramList = [SELECT Id FROM Program__c WHERE Name in :myNames];
Avoid SOQL queries that are inside FOR loops.
https://help.salesforce.com/articleView?id=000181404&type (https://help.salesforce.com/articleView?id=000181404&type=1)
Regards
Here's a version with bulkification:
I just added bulkification at the top of the code basically... let me know what you think.
I have not had the chance to try the code just yet.
I will be right back with you.