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
adam_purkissadam_purkiss 

Trigger: System.Exception: LHQ:Too many SOQL queries: 21

I've thought of everything I can here but just can't understand why this is happening.  This AppExchange ackage is certified - passed the security review.  Here's the trigger code that continues to error out with too much DML/SQL queries.  Any help would be greatly appreciated!

 

 

trigger LHQUpdateCampaignMembers on CampaignMember (before insert, before update, before delete) 
{
	if((Trigger.isBefore && Trigger.isInsert) || (Trigger.isBefore && Trigger.isUpdate))
	{
		List<String> conIds = new List<String>();
		List<String> ledIds = new List<String>();
		List<String> camIds = new List<String>();

		for(CampaignMember c : Trigger.new)
		{
			conIds.add(c.ContactId);
			ledIds.add(c.LeadId);
			camIds.add(c.CampaignId);
		}
	
		Contact[] conList = [SELECT Id, LHQUpdateContactBit__c FROM Contact WHERE Id IN :conIds];
		Lead[] ledList = [SELECT Id, LHQUpdateLeadBit__c, IsConverted FROM Lead WHERE Id IN :ledIds];
		Campaign[] camList = [SELECT Id, LHQUpdateCampaignBit__c FROM Campaign WHERE Id IN :camIds];
	
		for(Contact c : conList)
		{
			c.LHQUpdateContactBit__c = false;
		}

		// Prepare Lead list without converted leads
		List<Lead> ledList2 = new List<Lead>();
			
		for(Lead l : ledList)
		{
			if(!l.IsConverted)
			{
				Lead ll = l;
				ledList2.add(ll);
			}
		}

		for(Lead l : ledList2)
		{
			l.LHQUpdateLeadBit__c = false;
		}
			
		for(Campaign c : camList)
		{
			c.LHQUpdateCampaignBit__c = false;
		}
			
		update conList;
		update ledList2;
		update camList;
	}
}

 

 

imuino2imuino2

I did found an article that might be very helpful, http://techsahre.blogspot.com/2010/04/too-many-soql-queries-21.html.

adam_purkissadam_purkiss

Thank you imuino2.  I'm reveiwing it now and trying to see if this may be the case with me.  I didn't realize that a DML statement fired in one bulkified trigger counts against the DML statements fired in another bulkified trigger.  This makes things tremendously challenging since I need to know when members are added to a campaign in one trigger execution context (CampaignMembers object) and flag the individual Contact as well.  Each Contact will fire off another trigger when touched by this CampaignMember trigger.

 

I'll let you know what I discover.  Thanks again!

 

Adam