
Thank you for helping folks.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);
}
}
}
}
The query error is from a lack of bulkification, where you exceed the number of queries in a transaction. This happens since each lead inserted in a batch will be executing a new query, potentially 100+ queries just for this trigger, not counting whatever other code you have in that environment.Here's a version with bulkification:
I just added bulkification at the top of the code basically... let me know what you think.trigger ProcessWebFields on Lead (after insert, after update) {
// bulkify querying of programs
Map<String, Program__c> programs = new Map<String, Program__c>();
for (Lead l : Trigger.new) {
if (String.isNotBlank(l.Web_Program_of_Interest__c)) {
for (String programName : l.Web_Program_of_Interest__c.split(';')) {
programs.put(programName, null);
}
}
}
for (Program__c p : [select Id, Name from Program__c where Name in :programs.keySet()]) {
programs.put(p.Name, p);
}
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);
}
Program__c primaryProgram = umolProgramsArr.isNotEmpty() ? programs.get(umolProgramsArr[0]) : null;
if (primaryProgram) {
Utilities.setPrimaryProgramOfInterest(updatedLead.Id, primaryProgram.Name);
Utilities.checkPrimaryPOI(updatedLead.Id, primaryProgram.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);
}
}
}
}
}