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
SFDC-DMGSFDC-DMG 

Apex Code Error Upon Deployment in Production - Too many SOQL queries

Hi,

 

I'm am trying to depoy an apex trigger to production, and I am running into an error that's seemingly completely unrelated to the code I'm trying to deploy but it's preventing me from being able to do it.

 

I'm not sure why this is happening or what changes I can make to avoid this error, but any insight you have would be extremely helpful. I have no idea what else to do to fix it!

 

Here's the failure message I'm getting when I try to deploy the inbound change set and there is a failure:

 

API Name
Type
Line
Column
Problem

UnitTests_logAttempts.SingleLeadAttempts() Class 15 1 Failure Message: "System.Exception: Too many SOQL queries: 101", Failure Stack Trace: "Trigger.ActivityOppStageName2: line 15, column 1"

 

Here's the "Trigger.ActivityOppStageName2" apex code that's causing the problem:

 

trigger ActivityOppStageName2 on Task (before insert, before update){

// If related to an Opportunity, query to find out the Opportunity number

// Create collection of tasks that are related to an opp (where the opp is listed only once)
    Set<Id> opIds = new Set<Id>();
    for(Task t : trigger.new){
        String wId = t.WhatId;
        if(wId!=null && wId.startsWith('006') && !opIds.contains(t.WhatId)){
            opIds.add(t.WhatId);
        }
    }
    // Pull in opp ids and related field to populate task record
    List<Opportunity> taskOps = [Select Id, leadsource, lead_source_detail__c, StageName from Opportunity where Id in :opIds limit 1];
    System.assert(taskOps != null);
    Map<Id, Opportunity> opMap = new Map<Id, Opportunity>();
    for(Opportunity o : taskOps){
        opMap.put(o.Id,o);
    }
    // Update custom task field with custom opp field
    for(Task t : trigger.new){
        String wId = t.WhatId;
        if(wId!=null && wId.startswith('006')){
            Opportunity thisOp = opMap.get(t.WhatId);
            if(thisOp!=null){t.Sales_Stage__c = thisOp.StageName;} 
            if(thisOp!=null){t.Lead_source__c = thisOp.LeadSource;}
            if(thisOp!=null){t.record_type__c = 'Opportunity';}
            if(thisOp!=null){t.lead_source_detail__c = thisOp.Lead_source_detail__c;} 
        }
    }
}

 

Here's the "UnitTests_logAttempts.SingleLeadAttempts()" unit test class that's related to the problem:

 

       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);
            
      
           
        }
   }
   
   static testMethod void SingleLead_LiveDemoAttempt() {
    
        if(!runTest_SingleLead_LiveDemoAttempt == true) {
            System.debug('===== SingleLead_LiveDemoAttempt 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,
              Customer_Type__c = 'good',
              Leadsource = 'Sourced by sales',
              Lead_source_detail__c = 'sales',
              Lead_stage__c = 'Suspect',
              How_Do_They_Assess_Networks_Today__c = 'unknown');
              
            insert l;
            
            String sId = l.Id;
            String compareResult;
            
            // Test Live Demo
            compareResult = logAttempts.createAttempt('Live Demo', 'Lead', sId);
            
            // Query for a Task associated to the Lead
            Task newTask = 
              [Select WhoId, Type, Subject, Status, Id 
               From   Task 
               Where  WhoId = :sId
               Limit 1];
                        
          
            
        }
   }
   

 

 

If you need any further information to help, please let me know!!

 

Thank you!

 

netspidernetspider
You need to split the testmethod into separate ones

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



This should work

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;

// Inserting a Call Task should automatically update the Lead
l = ReGetLead(l.Id);

String sId = l.Id;
String compareResult;

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

static testMethod void SingleLeadAttempts2() {
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;

// Inserting a Call Task should automatically update the Lead
l = ReGetLead(l.Id);



String sId = l.Id;
String compareResult;
String sToday = Date.Today().format();

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

static testMethod void SingleLeadAttempts3() {
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;

// Inserting a Call Task should automatically update the Lead
l = ReGetLead(l.Id);



String sId = l.Id;
String compareResult;
String sToday = Date.Today().format();

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

static testMethod void SingleLeadAttempts4() {
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;

// Inserting a Call Task should automatically update the Lead
l = ReGetLead(l.Id);



String sId = l.Id;
String compareResult;
String sToday = Date.Today().format();


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



}