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
Mark GallagherMark Gallagher 

trigger testing too many SOQL queries

This trigger working the way I want it to, but I'm getting the too many SOQL queries error in my test class I've tried moving the queries out of the for loop because I know that can cause govenor limit hits, but I haven't done it properly help please.

<---Trigger Code--->
trigger LimitLeads on Lead (after update)
{
    List<Lead> ownerChange = new List<Lead>();
    for(Integer i = 0; i < Trigger.New.size(); i++)
    {
        if(Trigger.New[i].OwnerId != Trigger.old[i].OwnerId)
        {
            ownerChange.add(Trigger.New[i]);
        }
    }
    for(Lead ld: ownerChange)
    {
        if((ld.Status == '01-New' || ld.Status == '20-Nurture') && ld.RecordTypeId == '01250000000Mvl2AAC' && ld.Lead_Source_Type__c == 'Team Lead') 
        {
                Id LeadOwner = ld.OwnerId;
                List<Lead> trueLeads = [SELECT Id FROM Lead WHERE OwnerId =: LeadOwner AND 
                                       (Status = '01-New' OR Status = '20-Nurture') AND
                                       RecordTypeId = '01250000000Mvl2AAC' AND
                                       Lead_Source_Type__c = 'Team Lead'];
                List<User> capName = [SELECT Pipeline_Cap__c, Full_Name__c FROM User WHERE Id =: LeadOwner];
                Integer pipelineCap = (Integer) capName[0].Pipeline_Cap__c;
                String name = capName[0].Full_Name__c;
                Integer numLeads = trueLeads.size();
                Integer remove = numLeads - pipelineCap;
                if(numLeads > pipelineCap)
                {
                    ld.OwnerId.addError( name + ' too many team leads remove some to add more team leads');
                }
        }      
    }
}
Akshay_DhimanAkshay_Dhiman
Hi Mark,

Try this code , I have made changes accordingly,

Hope it will help you --
 
<---Trigger Code--->
trigger LimitLeads on Lead (after update)
{
Set<Id> LeadOwner = new Set<Id>();
List<Lead> trueLeads = new List<Lead>();
List<User> capName = new List<User>();

for(Integer i = 0; i < Trigger.New.size(); i++)
{
if(Trigger.New[i].OwnerId != Trigger.old[i].OwnerId && Trigger.New[i].OwnerId!=Null)
{
LeadOwner.add(Trigger.New[i].OwnerId);
}
}
if(LeadOwner.size()>0)
       {
trueLeads = [SELECT Id FROM Lead WHERE OwnerId =: LeadOwner AND 
         (Status = '01-New' OR Status = '20-Nurture') AND
         RecordTypeId = '01250000000Mvl2AAC' AND
         Lead_Source_Type__c = 'Team Lead'];
         
         capName = [SELECT Pipeline_Cap__c, Full_Name__c FROM User WHERE Id =: LeadOwner];
         
         if(trueLeads.size()>0)
         {
for(Lead ld: trueLeads)
   {
if(ld.Status!=Null && ld.RecordTypeId!=Null)
  {
if((ld.Status == '01-New' || ld.Status == '20-Nurture') && ld.RecordTypeId == '01250000000Mvl2AAC' && ld.Lead_Source_Type__c == 'Team Lead') 
 {
     
 Integer pipelineCap = (Integer) capName[0].Pipeline_Cap__c;
 String name = capName[0].Full_Name__c;
 Integer numLeads = trueLeads.size();
 Integer remove = numLeads - pipelineCap;
 if(numLeads > pipelineCap)
  {
  ld.OwnerId.addError( name + ' too many team leads remove some to add more team leads');
  }
 }      
  }
   }
      }
        }
}


Thanks 
Akshay