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
Marc Mastrocola 26Marc Mastrocola 26 

Need Help With Apex Error

Previous employee wrote Trigger/Class and it's causing an issue during lead conversion (See screenshot). Any help on how to fix would be greatly appreciated!
Error Message

Here is the code of the Class:
 
public class Kash_Task {
  public static void processUpdate(Task[] tasks_new, Task[] tasks_old) {        
      
        // update related LEAD for this task if applicable
        String LeadPREFIX = Schema.SObjectType.Lead.getKeyPrefix();
        
        if (tasks_new != null) {
            for(integer i=0; i<tasks_new.size(); i++) {
                Task t = tasks_new[i];
                if (t.WhoId!=null && ((String)t.WhoId).startsWith(LeadPREFIX)) {
                   Kash_Task.updateLead_NextAction(t.WhoId);
                }            
            }
        }
        
        if (tasks_old != null) {
            for(integer i=0; i<tasks_old.size(); i++) {
                Task t = tasks_old[i];
                if (t.WhoId!=null && ((String)t.WhoId).startsWith(LeadPREFIX)) {
                   Kash_Task.updateLead_NextAction(t.WhoId);
                }            
            }
        }
    }
    
    public static void  updateLead_NextAction(ID LeadId) {
        Lead l = [SELECT Id, Status, OwnerId FROM Lead WHERE Id=:LeadId];        
        
        // set next open task
        List<Task> nextTaskList = [SELECT Id, ActivityDate, Subject From Task Where (WhoId=:LeadId AND isDeleted=false AND Status='Open') ORDER BY ActivityDate ASC LIMIT 1];
        if (!nextTaskList.isEmpty()) {
            Task nextTask = nextTaskList[0];
          l.Next_Action__c = nextTask.Subject;
          l.Next_Action_Date__c  = nextTask.ActivityDate;
        }
        else {
            l.Next_Action__c = null;
          l.Next_Action_Date__c  = null;
        }
        
        // set last completed task
        List<Task> lastTaskList = [SELECT Id, ActivityDate, Subject From Task Where (WhoId=:LeadId AND isDeleted=false AND Status='Completed') ORDER BY ActivityDate DESC LIMIT 1];
        if (!lastTaskList.isEmpty()) {
            Task lastTask = lastTaskList[0];
          l.Last_Action__c = lastTask.Subject;
          l.Last_Action_Date__c  = lastTask.ActivityDate;
        }
        else {
            l.Last_Action__c = null;
          l.Last_Action_Date__c  = null;
        }

        update l;
    }
}

Here is the Trigger:
 
trigger Kash_Task_Trigger on Task (after insert, after update, after delete, after undelete) {
    if (trigger.isInsert || trigger.isUndelete) {
        Kash_Task.processUpdate(trigger.new, null);        
    }
    else if (trigger.isUpdate) {
        Kash_Task.processUpdate(trigger.new, trigger.old);
    }
    else if (trigger.isDelete) {
        Kash_Task.processUpdate(null, trigger.old);
    }


 
Best Answer chosen by Marc Mastrocola 26
FearNoneFearNone
hi marc,

it means that more than 100 queries have been executed. You can only query up to 100 times.
Since "updateLead_NextAction" is inside a loop, it is easy to reach the limit.

Here is a better approach:
try to query all the data and save those in a variable, then "inside updateLead_NextAction", search the needed in the variable.
it is also better to use map-variable for easier searching.
public class Kash_Task {

  Map<ID, Lead> mapLead;
  Map<ID, Task> mapTask;
  
  public static void processUpdate(Task[] tasks_new, Task[] tasks_old) {        
        mapLead = new Map<ID, Lead>();
        mapTask = new Map<ID, Task>();
        
        List<Id> leadIds = get all t.WhoId in tasks via loop perhaps
        mapLead = select * from Lead where ID in leadIds
        mapTask = select * from Task where isDeleted=false AND (Status='Completed' OR Status='Open')
        
        if (tasks_new != null) {
            ...
    }
    
    public static void  updateLead_NextAction(ID LeadId) {
        Lead l = mapLead where key is leadId       
        
        // set next open task
        List<Task> nextTaskList = find a way to search in mapTask
        if (!nextTaskList.isEmpty()) {
            ...
        
        // set last completed task
        List<Task> lastTaskList = = find a way to search in mapTask
        if (!lastTaskList.isEmpty()) {
            ...

        update l;
    }
}

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_map_sobject.htm

Hope this will help you somehow.

All Answers

Marc Mastrocola 26Marc Mastrocola 26
Error message: Error: System.LimitException: Too many SOQL queries: 101 Class.Kash_Task.updateLead_NextAction: line 30, column 1 Class.Kash_Task.processUpdate: line 20, column 1 Trigger.Kash_Task_Trigger: line 6, column 1
FearNoneFearNone
hi marc,

it means that more than 100 queries have been executed. You can only query up to 100 times.
Since "updateLead_NextAction" is inside a loop, it is easy to reach the limit.

Here is a better approach:
try to query all the data and save those in a variable, then "inside updateLead_NextAction", search the needed in the variable.
it is also better to use map-variable for easier searching.
public class Kash_Task {

  Map<ID, Lead> mapLead;
  Map<ID, Task> mapTask;
  
  public static void processUpdate(Task[] tasks_new, Task[] tasks_old) {        
        mapLead = new Map<ID, Lead>();
        mapTask = new Map<ID, Task>();
        
        List<Id> leadIds = get all t.WhoId in tasks via loop perhaps
        mapLead = select * from Lead where ID in leadIds
        mapTask = select * from Task where isDeleted=false AND (Status='Completed' OR Status='Open')
        
        if (tasks_new != null) {
            ...
    }
    
    public static void  updateLead_NextAction(ID LeadId) {
        Lead l = mapLead where key is leadId       
        
        // set next open task
        List<Task> nextTaskList = find a way to search in mapTask
        if (!nextTaskList.isEmpty()) {
            ...
        
        // set last completed task
        List<Task> lastTaskList = = find a way to search in mapTask
        if (!lastTaskList.isEmpty()) {
            ...

        update l;
    }
}

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_map_sobject.htm

Hope this will help you somehow.
This was selected as the best answer