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
Paulo PerezPaulo Perez 

Number of SOQL queries: 101 out of 100 when run class


hello 
I need assitance, when i run the class sow me the message "Number of SOQL queries: 101 out of 100". Anybody could help me? :(

public class PacoteVitron{

    public static void ContatoTelefone() {
        List<Lead> accList = [Select Id from lead];
        PacoteVitron.Atendimento(accList);  
    }

   
    public static void Atendimento(Lead[] Indicacao) {
        List<Lead> LeadToUpdate = new List<Lead>();     
        for(Lead ll : Indicacao) {
            AggregateResult[] tarefa=[
            Select max(Createddate) ultima from task where Subject ='Ligação Call Center Fastcall' and whoid=:ll.id
            group by whoid
            ];
            if(tarefa.size() > 0){
            if ((datetime)tarefa[0].get('ultima')!=null){
            datetime dt=(datetime)tarefa[0].get('ultima');          
            ll.Data_da_ultima_ligacao__c=date.newinstance(dT.year(), dT.month(), dT.day());        
            LeadToUpdate.add(ll);      }} 
        }
        update LeadToUpdate;
    }
}
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Paulo,

Avoid SOQL query inside loop. Please try below code.
 
public class PacoteVitron{

    public static void ContatoTelefone() {
        List<Lead> accList = [Select Id from lead];
        PacoteVitron.Atendimento(accList);  
    }

   
    public static void Atendimento(Lead[] Indicacao) {
        List<Lead> LeadToUpdate = new List<Lead>();    
		AggregateResult[] tarefa=[
            Select max(Createddate) ultima from task where Subject ='Ligação Call Center Fastcall' and whoid=:ll.id
            group by whoid
            ];
        for(Lead ll : Indicacao) {
            
            if(tarefa.size() > 0){
            if ((datetime)tarefa[0].get('ultima')!=null){
            datetime dt=(datetime)tarefa[0].get('ultima');          
            ll.Data_da_ultima_ligacao__c=date.newinstance(dT.year(), dT.month(), dT.day());        
            LeadToUpdate.add(ll);      }} 
        }
        update LeadToUpdate;
    }
}

Let us know if it helps you.
 
Paulo PerezPaulo Perez
Ashish_Sharma_DEVSFDC, it does not work because ll.id in AggregateResult[].
Paulo PerezPaulo Perez
And i need for each lead, the max createddate from the task
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Paulo,

Please try below code.It collects all lead ids first and then uses those in query.
public class PacoteVitron{

    public static void ContatoTelefone() {
        List<Lead> accList = [Select Id from lead];
        PacoteVitron.Atendimento(accList);  
    }

   
    public static void Atendimento(Lead[] Indicacao) {
        List<Lead> LeadToUpdate = new List<Lead>();
		Set<Id> leadIdsSet = new Set<Id>();
		for(Lead ll : Indicacao){	
				leadIdsSet.add(ll.id);
		}
		AggregateResult[] tarefa=[
            Select max(Createddate) ultima from task where Subject ='Ligação Call Center Fastcall' and whoid in :leadIdsSet
            group by whoid
            ];
        for(Lead ll : Indicacao) {
            
            if(tarefa.size() > 0){
            if ((datetime)tarefa[0].get('ultima')!=null){
            datetime dt=(datetime)tarefa[0].get('ultima');          
            ll.Data_da_ultima_ligacao__c=date.newinstance(dT.year(), dT.month(), dT.day());        
            LeadToUpdate.add(ll);      }} 
        }
        update LeadToUpdate;
    }
}

Let us know if it helps you.