You need to sign in to do that
Don't have an account?
iKnowSFDC
Too Many SOQL Queries Error
I'm calling a static method from a trigger. I had all the of the methods in one originally, but had to change it to support bulk processing. I'm using a design pattern I've used elsewhere successfully, however for some reason, I'm not getting past the initial queries in the method. I'm not seeing why that's happening and am hoping someone has a suggestion.
Thanks!
JoAnn
Trigger:
trigger notifyPtnrOfNewLead on Lead (after insert, after update) { List<Lead> leadsInTrigger = new List<Lead>(); String[] ptnrs= new List<String>(); if(!controlTriggerRecursion.hasAlreadyLooped()){ for(Lead l: trigger.new){ if(l.Preferred_Partner_for_Tech_Purchases__c.length()>0){ leadsIntrigger.add(l); ptnrs.add(l.Preferred_Partner_for_Tech_Purchases__c); } } } controlTriggerRecursion.setHasAlreadyLooped(); ptrCampaignHandling.findPtrCampaign(leadsInTrigger,ptnrs); }
Class & Method:
public class ptrCampaignHandling{ public static void findPtrCampaign(List<Lead> ptrLead, String[] cmpaignPtr){
Map<String, Campaign> ptnrCampaignMap = new Map<String, Campaign>(); //This Query and the one for Accounts below are looping, but I'm not understanding why...
List<Campaign> camps = [SELECT id, Name, Campaign_Partner__c, Primary_Partner_Contact__c, Email_Template_Name__c, isActive, StartDate, EndDate, Type, Primary_Partner_Contact__r.Email FROM Campaign WHERE Type = 'Partners' AND IsActive = True]; for(Campaign cmp : camps){ ptnrCampaignMap.put(cmp.Name,cmp); } Map<String, Id> cmpPartnerId = new Map<String, Id>(); List<Account> ptrAccts = [SELECT id, Name FROM Account WHERE Type = 'Partner' AND Partner_Status__c = 'Agreement Signed' ORDER by CreatedDate DESC]; for(Account pa: ptrAccts){ cmpPartnerId.put(pa.Name, pa.id); } Map<Lead, Id> cmsToCreate = new Map<Lead, Id>(); List<Lead> newCampaignsToCreate = new List<Lead>(); for(Lead newLead : ptrLead){ String campName = newLead.pi_campaign__c + ' - ' + newLead.Preferred_Partner_for_Tech_Purchases__c; Campaign ptnrCampaign = ptnrCampaignMap.get(campName); if(ptnrCampaign != NULL){ cmsToCreate.put(newLead, ptnrCampaign.id); } else { newCampaignsToCreate.add(newLead); } } insertCamp(newCampaignsToCreate, cmpPartnerId); createNewCampMembers(cmsToCreate); } public static void insertCamp(Lead[] newCampsToCreate, Map<String, Id> cmptrList){ List<Campaign> newCampsToInsert = new List<Campaign>(); String[] CampaignPartners; for(Lead l : newCampsToCreate){ String campName = l.pi_campaign__c + ' - ' + l.Preferred_Partner_for_Tech_Purchases__c; Campaign newPtnrCampaign = new Campaign(); newPtnrCampaign.Name = campName; newPtnrCampaign.Type = 'Partners'; newPtnrCampaign.StartDate = date.today(); newPtnrCampaign.EndDate = date.today().addMonths(3); newPtnrCampaign.Campaign_Partner__c = newPtnrCampaign.Primary_Partner_Contact__c = cmptrList.get(l.Preferred_Partner_for_Tech_Purchases__c); newPtnrCampaign.isActive = true; newPtnrCampaign.Email_Template_Name__c = l.pi_campaign__c + ' - Lead Notification'; newcampsToInsert.add(newPtnrCampaign); campaignPartners.add(cmptrList.get(l.Preferred_Partner_for_Tech_Purchases__c)); } insert newCampsToInsert; findPtrCampaign(newCampsToCreate, campaignPartners); } public static void createNewCampMembers(Map<Lead, Id> newCM){ CampaignMember cm = new CampaignMember(); List<CampaignMember> cmsToInsert = new List<CampaignMember>(); for(Lead l : newCM.keySet()){ cm.CampaignId = newCM.get(l); cm.LeadId = l.id; cm.Status = 'Responded'; cmsToInsert.add(cm); } insert cmsToInsert; } }
I think the issue is with the following line in insertCamp function. This line is recursively calling the parent function and hence you are running into this issue. Try commenting the line and run your process and hopefully it will resolve the issue.
If you need to call the parent function again then have another static set to store the processed ids so that you can end the loop once you processed all possible combinations.
findPtrCampaign(newCampsToCreate, campaignPartners);
If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.
All Answers
I think you have miss placed brackets @ trigger.
Try this
Hi Avi,
Thanks for the feedback - moved the bracket as suggested, however still getting the same error. I'm still looping on the same two queries.
I think the issue is with the following line in insertCamp function. This line is recursively calling the parent function and hence you are running into this issue. Try commenting the line and run your process and hopefully it will resolve the issue.
If you need to call the parent function again then have another static set to store the processed ids so that you can end the loop once you processed all possible combinations.
findPtrCampaign(newCampsToCreate, campaignPartners);
If this post solves your problem kindly mark it as solution. if this post is helpful please throw Kudos.
Hi,
Yes, CodeWizard said right one.
Try to call the static boolean variable from class and try to avoid it from recursive calling.
Regards,
Rajesh.
Thank you all that had suggestions - it was a recursiveness issue. Fixing that solved this issue and another one I has having with a similiar development pattern.
JoAnn