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
sc2510sc2510 

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!

colemabcolemab

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.

JHayes SDJHayes SD

Agreed, I bet there's another trigger on Lead.

Shiv ShankarShiv Shankar
I also agreed....
SRKSRK

Moniter the code in debug log it will be grate if you can provide us the debug log

 

 

Shailesh DeshpandeShailesh Deshpande
You probably have another trigger/ workflo field updates:

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).
SRKSRK
Once he share the complete debug log we can figure that out easily
sc2510sc2510

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'

 

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

}

 

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!

SRKSRK

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

sc2510sc2510

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?

 

trigger SaveRecentCampaign on Lead (after update) {
    // Query for all the lead information
    Set<Id> leadsIds = new Set<Id>();
    for(Lead l : trigger.new) {
        Lead oldLead = Trigger.oldMap.get(l.ID);
        if (l.Non_PURL_Lead__c != oldLead.Non_PURL_Lead__c && l.Non_PURL_Lead__c==true) {
            leadsIds.add(l.Id);
        }
    }
    if(leadsIds.size()>0){
        // Query the CampaignMember for their associated recentCampaign and place the results in a map.
        Map<Id, CampaignMember> recentCampaigns = new Map<Id, CampaignMember>([SELECT Id, CampaignID, LeadID, HasResponded, LastModifiedDate, Campaign_Type__c FROM CampaignMember WHERE LeadID in :leadsIds AND HasResponded=True ORDER BY LastModifiedDate DESC NULLS LAST]);
        
        List<Lead> leadsOld = [SELECT Id, Admin_Recent_Campaign_Source__c, Admin_Recent_Lead_Source__c FROM Lead WHERE Id in :LeadsIds];
        
        // Now use the map to set the appropriate recentCampaign on every Lead processed by the trigger.
        // Change all the lead records
        List<Lead> leadsNew = new List<Lead>{};
        for (Lead l : leadsOld) {    
            if(recentCampaigns.get(l.Id).Id != null){
                l.Admin_Recent_Campaign_Source__c=recentCampaigns.get(l.Id).CampaignID;
                l.Admin_Recent_Lead_Source__c=recentCampaigns.get(l.Id).Campaign_Type__c;
                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;
        }
    }
}

 

I think the problem is within the trigger but let me know if the test code would help. Thanks again for any replies!