You need to sign in to do that
Don't have an account?
SFDC-DMG
Preventing a Loop
Hello,
I have the following trigger and it keeps causing this error: "System.Exception: Too many SOQL queries: 101". Can anyone help me to update this code so that it is not in a loop? I've been trying and struggling with it a bit.
Thank you!
Danielle
trigger LeadOppStageName on Task (before insert, before update, after insert){
// Goal: Pull fields from the lead record for tracking activities related to lists and to discern in which lead stages these activities occured
// If related to a Lead, query to find out the Lead ID, Lead Stage, and Lead Source Detail
// Create collection of tasks that are related to an lead (where the lead is listed only once)
Set<Id> leadIds = new Set<Id>();
for(Task t : trigger.new){
String wId = t.WhoId;
if(wId!=null && wId.startsWith('00Q') && !leadIds.contains(t.whoId)){
leadIds.add(t.WhoId);
}
}
// Pull in lead ids and related field to populate task record
List<Lead> taskLead = [Select Id, leadsource, lead_source_detail__c, Lead_Stage__c, Lead_Channel__c, Lead_Partner_Program__c, Lead_Segment__c,
Original_Lead_Channel__c, Original_Lead_Partner_Program__c, Original_Lead_Segment__c from Lead where Id in :leadIds limit 1];
Map<Id, Lead> leadMap = new Map<Id, Lead>();
for(Lead l : taskLead){
leadMap.put(l.Id,l);
}
// Update custom task field with custom lead field
for(Task t : trigger.new){
String wId = t.WhoId;
if(wId!=null && wId.startswith('00Q')){
Lead thisLead = leadMap.get(t.WhoId);
if(thislead!=null){t.Sales_Stage__c = thisLead.Lead_Stage__c;}
if(thislead!=null){t.Lead_Source__c = thisLead.LeadSource;}
if(thislead!=null){t.record_type__c = 'Lead';}
if(thislead!=null){t.lead_source_detail__c = thislead.Lead_source_detail__c;}
if(thislead!=null){t.Lead_Channel__c = thislead.lead_channel__c;}
if(thislead!=null){t.Original_Lead_Channel__c = thislead.original_lead_channel__c;}
if(thislead!=null){t.lead_partner_program__c = thislead.lead_partner_program__c;}
if(thislead!=null){t.Original_lead_partner_program__c = thislead.Original_lead_partner_program__c;}
if(thislead!=null){t.lead_segment__c = thislead.lead_segment__c;}
if(thislead!=null){t.Original_lead_segment__c = thislead.Original_lead_segment__c;}
}
}
}
is it kicking when you are uploading the files through data loader.
If so reduce the batch size as much as you can.
Even if you are doing some thing with batch class, reduce the batch size.
The way I would approach is by checking the debug logs and see what is going on there,I mwan some other transacion running in the same Context or not.
The query in your trigger is not in a loop, which is good, but perhaps the code that is causing the trigger to fire is inside a loop. Does the error happen when only one Task is touched?
Otherwise, as previous posters suggested, review the Debug Logs to see what else is happening in your transaction.
Certainly this is not the only code running and causing the exception, somewhere else the task is getting created too many times and causing this trigger to fire again and again, check the same.
Thank you guys for your help on this. I guess I'm realizing that it's probably not this trigger to cause the error as much as it is this in combindation with the test clsss language that is causing the error.
If you guys wouldn't mind taking a look at the rest class, which is where the error is reported, to see where this loop is being reported, that would be great (this is just one method that's part of a bigger test class, but please let me know if you need to see more):