Skip to main content
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:

 

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.
3 answers
  1. Sep 15, 2017, 8:50 PM
    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:

     

    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);

    }

    }

    }

    }

    }

    I just added bulkification at the top of the code basically... let me know what you think.
0/9000