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
Elliot32Elliot32 

Task Trigger - Error: Too many SOQL queries: 101

Thanks for taking a look at my trigger, the goal of the trigger is the following:

Whenever a Task where Type == 'Call' and Status == 'Completed' and the WhoId references to a Lead is either inserted, updated, deleted or undeleted, to take a Lead field called Last_Transfer_Date_Time__c (a datetime field) and pass it to the Task field Last_Transfer_Date_Time__c (datetime); the whole purpose of this is for the query. After this happens, I would like to sum up all Tasks that fit the afforementioned criteria, as well as a formula boolean field on the Task called Task_this_Cycle__c (this checks true when Activity_Date_Time__c [the datetime of the activity] is greater than Last_Transfer_Date_Time__c).

However, I am running into the too many queries error and having been banging my head against my keyboard for the past few days trying to resolve. My ultimate goal is to be able to roll-up all Tasks where Type == 'Call' and Status == 'Completed' where the Task was created after the Last Transfer Date Time of the Lead (in case my strategy is off base). Any help is much appreciated! Thanks, Elliot.
 
trigger EPS_CallsSinceTransfer_v3 on Task (after insert, after update, after delete, after undelete) {

    List<Id> leadIdList = new List<Id>();
    
    If (Trigger.isInsert || Trigger.isUndelete) {
        For (Task t : Trigger.new) {
            If (t.WhatId == null && t.WhoId != null && t.WhoId.getSObjectType() == Lead.SObjectType && t.Status == 'Completed' && t.Type == 'Call') {
                leadIdList.add(t.WhoId);
            }
        }
    }
    
    If (Trigger.isDelete || Trigger.isUpdate) {
        For (Task t : Trigger.old) {
            If (t.WhatId == null && t.WhoId != null && t.WhoId.getSObjectType() == Lead.SObjectType && t.Status == 'Completed' && t.Type == 'Call') {
                leadIdList.add(t.WhoId);
            }
        }
    }
    
    If (leadIdList.isEmpty() == false) {
    
        List<Lead> leadsWithTasks = [SELECT Id, Calls_this_Cycle__c, Last_Transfer_Date_Time__c FROM Lead WHERE Id IN : leadIdList];
        List<Lead> leadsToUpdate = new List<Lead>();
        List<Task> tasksToCount = new List<Task>();
        List<Task> tasksToUpdate = new List<Task>();
        
        For (Lead ld : leadsWithTasks) {
            
            For (Task t : [SELECT Id, Type, Status, Task_this_Cycle__c FROM Task WHERE Type = 'Call' AND Status = 'Completed' AND (WhoId IN : leadsWithTasks)]) {
            
                t.Last_Transfer_Date_Time__c = ld.Last_Transfer_Date_Time__c;
                tasksToUpdate.add(t);
            }           

            update tasksToUpdate;
            
            For (Task t : tasksToUpdate) {
                If (t.Task_this_Cycle__c == true) {
                    tasksToCount.add(t);
                }
            }
        
            ld.Calls_this_Cycle__c = tasksToCount.size();
            leadsToUpdate.add(ld);
        
        }
        
        If (leadsToUpdate.isEmpty() == false) {
            update leadsToUpdate;
        }
    }
}