+ Start a Discussion
SFDC-DMGSFDC-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;}            

}
}
}
Suresh RaghuramSuresh Raghuram

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.

Vinit_KumarVinit_Kumar

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.

MJ Kahn / OpFocusMJ Kahn / OpFocus

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.

ibtesamibtesam

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.

SFDC-DMGSFDC-DMG

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):

 

       static testMethod void SingleLeadAttempts() {
        
        if(!runTest_SingleLeadAttempts == true) {
            System.debug('===== SingleLeadAttempts unit test is currently inactive; enable before release');
        } else {
            
            Lead l = new Lead(FirstName = 'Bob',
              LastName = 'Test',
              Company = 'TestCo',
              Email = 'btest@testco.com',
              Title = 'Captain',
              Number_of_Customers__c = 10,
              Leadsource = 'Sourced by Sales',
              Lead_source_detail__c = 'sales',
              Customer_Type__c = 'good',
              How_Do_They_Assess_Networks_Today__c = 'unknown',
              lead_stage__c = 'Suspect',
              Status='None');
              
            insert l;
            
            String sToday = Date.Today().format();
            // Insert a call task
            Task t = new Task(
              WhoId = l.Id,
              whatid = null,
              Subject = 'Call ' + sToday + ' 9:45 AM',
              CallType = 'Outbound',
              CallObject = '924892',
              Type = 'Call',
              Status = 'Completed',
              
              ActivityDate = Date.Today());
              
            insert t;
            
            // Inserting a Call Task should automatically update the Lead
            l = ReGetLead(l.Id);
            
           
            
            String sId = l.Id;
            String compareResult;

            t = new Task(
              WhoId = l.Id,
              whatid = null,
              Subject = 'Call with my mother',
              Type = 'Call',
              Status = 'Completed',          
              ActivityDate = Date.Today());
              
            insert t;
            
            l = ReGetLead(l.Id);
          

            t = new Task(
              WhoId = l.Id,
              whatid = null,
              Subject = 'Call with my mother',
              CallType = 'Outbound',
              CallObject = '924892',
              Type = 'Call',
              Status = 'Completed',          
              ActivityDate = Date.Today());
              
            insert t;
            
            l = ReGetLead(l.Id);
            

            t = new Task(
              WhoId = l.Id,
              whatid = null,
              Subject = 'Call',
              CallType = 'Outbound',
              CallObject = '924892',
              Type = 'Call',
              Status = 'Completed',          
              ActivityDate = Date.Today());
              
            insert t;
            
            l = ReGetLead(l.Id);
         

            t = new Task(
              WhoId = l.Id,
              whatid = null,
              Subject = 'Call ' + sToday + ' 9:45 AM',
              CallType = 'Outbound',
              CallObject = '924892',
              Type = 'Call',
              Status = 'Not Started',
              
              ActivityDate = Date.Today());
              
            insert t;
        
            l = ReGetLead(l.Id);            
            // This should fail as Status <> 'Completed'
           

            t = new Task(
              WhoId = l.Id,
              whatid = null,
              Subject = 'Call ' + sToday + ' 9:45 AM',
              CallType = 'Outbound',
              CallObject = '924892',
              Type = 'Call',
              Status = 'Completed',
              
              ActivityDate = Date.Today());
              
            insert t;
        
            l = ReGetLead(l.Id);            
         

            // Test Connect                         
            compareResult = logAttempts.createAttempt('Connect', 'Lead', sId);
            
            l = ReGetLead(l.Id);
            
           
            
            // Test Profile
            compareResult = logAttempts.createAttempt('Profile', 'Lead', sId);
            
            l = ReGetLead(l.Id);
            
      
           
        }
   }