function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KeithlarsKeithlars 

Too many SOQL queries: 21

Every once in a while I get the following error from my trigger:

 

 Apex script unhandled trigger exception by user/organization: 00530000000fJTi/00D300000000beQ

Contact_Area_of_Interest: execution of BeforeUpdate

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

Trigger.Contact_Area_of_Interest: line 18, column 9

 

 Can anyone see where my problem is in the code below.

 

Thanks.

 

 

trigger Lead_Area_of_Interest on Lead(before insert, before update, after insert, after update) { String aoiValue = ''; // Set up list to hold unique IDs of all new or updated leads Set<Id> leadIds = New Set<Id>(); // Set up list to hold unique area of interests Set<String> interests = new Set<String>(); // Iterate through array of new or updated records for (Lead a : trigger.new){ //if (a.area_of_interest_landing__c != null && a.area_of_interest_landing__c != '') leadIds.add(a.id); } // Map the area of interest field values to ID Map<ID, Lead> aoi = new Map<Id, Lead>( [select area_of_interest__c, area_of_interest_landing__c from Lead where ID in : leadIds]); if (Trigger.isBefore) { if (Trigger.isDelete) { // In a before delete trigger, the trigger accesses the records that will be // deleted with the Trigger.old list. for (Lead a : Trigger.old) { if (a.firstname == 'delete') { a.addError('You cannot delete this record!'); } } } else { // In before insert or before update triggers, the trigger accesses the new records // with the Trigger.new list. for (Lead a : Trigger.new) { if (a.firstname == 'bad') a.firstname.addError('Bad name'); if (a.area_of_interest_landing__c != null && a.area_of_interest_landing__c != '') { if (a.area_of_interest__c != null && a.area_of_interest__c != '') { // if here then we have to merge both area of interest fields // Add all values from both fields into Set array which ensures uniqueness for (Lead aoiLanding : trigger.new) { String[] result1 = a.area_of_interest_landing__c.split(';'); for (String x : result1) { interests.add(x); } String[] result2 = a.area_of_interest__c.split(';'); for (String y : result2) { interests.add(y); } } // Populate variable to be used in update Integer i = 0; for (String s : interests) { if (i == 0) aoiValue = s; else aoiValue = aoiValue + ';' + s; i++; } try { a.area_of_interest__c = aoiValue; } catch (DmlException de) { for (Integer e = 0; E < de.getNumDml(); e++) { //NC a.area_of_interest__c.addError(de.getDmlMessage(e)); //NC } } } else { // area_of_interest__c is null and no record in aoi map so just copy values a.area_of_interest__c = a.area_of_interest_landing__c; } } else { // no exising values so just copy landing field values if not null TEST5 if (a.area_of_interest_landing__c != null && a.area_of_interest_landing__c != '') { // try { a.area_of_interest__c = aoi.get(a.id).area_of_interest_landing__c; //NC // } // catch (DmlException de) { // for (Integer e = 0; E < de.getNumDml(); e++) { //NC // a.area_of_interest__c.addError(de.getDmlMessage(e)); //NC // } // } } } } if (Trigger.isInsert) { // area_of_interest__c is null and no record in aoi map so just copy values for (Lead a : Trigger.new) { if (a.area_of_interest_landing__c != null && a.area_of_interest_landing__c != '') { // try { a.area_of_interest__c = a.area_of_interest_landing__c; // } // catch (DmlException de) { // for (Integer e = 0; E < de.getNumDml(); e++) { //NC // a.area_of_interest__c.addError(de.getDmlMessage(e)); //NC // } // } } } // If the trigger is not a before trigger, it must be an after trigger. } else { /* if (Trigger.isInsert) { List<Contact> contacts = new Contact[0]; for (Lead a : Trigger.new) { if(a.lastname == 'makeContact') { contacts.add(new Contact (firstname = a.firstname, lastname = a.lastname, accountId = a.id)); } } insert contacts; } */ } }}}

 

 

 

Nathan CrosbyNathan Crosby
The code snippet is for the trigger named Lead_Area_of_Interest and the error message references a different trigger (Contact_Area_of_Interest).
JimRaeJimRae

Couple of things I noticed about the trigger code you did post, in addition to it not matching the error message name you originally listed.

It has a section of code for trigger.isdelete, but it is not a delete trigger,so that code won't fire.  Also, you are doing before and after which means most of the code will execute 4 times per object.  If you don't need it to be both before and after, pick whichever one is most relevant to your use case.