You need to sign in to do that
Don't have an account?
sc2510
apex beginner - trigger exception: too many SOQL queries
I've looked at the other posts regarding this but can't figure out what I'm doing wrong. I'm trying to get a lead field updated when a campaign member is added with a status of responsed (starts with a responded status, for campaigns other than webinars and tradeshows).
I get this error via email when I try to mass add a member list of responses: SaveAdminActivate: System.Exception: Too many SOQL queries: 101
trigger SaveAdminActivate on CampaignMember (after insert) { // Query for all the contact information List<Id> leadsIds = new List<Id>{}; for(CampaignMember member : trigger.new) { if (member.Status=='Responded' && member.Campaign_Type__c != 'Webinar' && member.Campaign_Type__c != 'Tradeshow / Conference' && member.LeadId != null) { leadsIds.add(member.LeadId); } } List<Lead> leadsOld = [SELECT Id FROM Lead WHERE Id in :LeadsIds]; // Change all the lead records List<Lead> leadsNew = new List<Lead>{}; for (Lead l : leadsOld) { l.Non_PURL_Lead__c=true; l.Apex_Context__c=true; // perform some update to lead record leadsNew.add(l); } // Update all the queries at once if(!leadsNew.isEmpty()){ update leadsNew; } }
Thanks in advance for any replies!
Usually this error occurs because of a SOQL query inside of some kind of loop (usually a for loop).
However, I don't see that situation in your code.
Are you sure you don't have other code (such as another trigger) that is being called by this code and is causing the error due to a SOQL query in a loop?
Perhaps an update related trigger on the lead object?
A detailed review of the log that has the error would probably tell you where those SOQL queries are being used up.
Agreed, I bet there's another trigger on Lead.
Moniter the code in debug log it will be grate if you can provide us the debug log
1. trigger On Insert of campaignMember OR
2. Trigger on Update of Leads. OR
3. Workflow field update on campaignMember which might be causing some other trigger written on update of campaignMember OR
4. Workflow field update on leads which might be causing trigger on update of leads
5. Workflow Task causing some trigger on tasks to fire.
If there are triggers, check of you could modify those triggers. Also it might be a case that those triggers are inturn causing another trigger to fire. You need tto modify even those.
if doing this still doesnt resolve your issue, you might need to use a static boolean variable to stop other triggers from executing (not recommended).
Thank you for all the replies!
I checked the logs...I'm not really sure what I'm looking for but I noticed another trigger was initiated that I didn't think would be. Is it redundant to use both of these? I assumed this one would only get triggered if the status started as 'Sent' and was changed to 'Responded'
Here's a link to all of the Debug Logs generated: https://www.yousendit.com/download/UVJqeW4ycWZnYU44SjhUQw (let me know if anyone has trouble accessing them or if there's a better way of sharing them). These show up when I run a tool that tries to add about 450 leads/contacts to a campaign with a status of responded, the first three Api requests show a success status, then five show up with a status of 'Too many SOQL queries: 101' (files 1-5). About a minute later two more show up with a status of 'Before Insert or Upsert list must not have two identically equal elements' (files 6-7).
Thanks again for any and all replies!
i had modify your trigger a little so it will make not make any soql if status is not change
trigger SaveAdminActivateLead on CampaignMember (after update)
{
// Query for all the contact information
List<Id> leadsIds = new List<Id>{};
for(CampaignMember member : trigger.new)
{
CampaignMember oldcm = Trigger.oldMap.get(member.ID);
if (member.Status != oldcm.Status && member.Status=='Responded' && member.Campaign_Type__c != 'Webinar' && member.Campaign_Type__c != 'Tradeshow / Conference' && member.LeadId != null)
{
leadsIds.add(member.LeadId);
}
}
if(leadsIds.size()>0)
{
List<Lead> leadsOld = [SELECT Id FROM Lead WHERE Id in :LeadsIds];
// Change all the lead records
List<Lead> leadsNew = new List<Lead>{};
for (Lead l : leadsOld) {
l.Non_PURL_Lead__c=true;
l.Apex_Context__c=true;
// perform some update to lead record
leadsNew.add(l);
}
// Update all the queries at once
if(!leadsNew.isEmpty()){
update leadsNew;
}
}
}
Thank you for the help!
I am now seeing an issue (the tests fail) with bulkifying a related trigger (above trigger activates this one). Does this look correct? Can someone point me in the right direction?
I think the problem is within the trigger but let me know if the test code would help. Thanks again for any replies!