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
Mayank.msMayank.ms 

Developer script exception: System.LimitException: Too many query rows: 50001"

Hi, I have a task trigger that updates the opportunity fields but i got the error email:

"Developer script exception from Webgility : activityCountUpdate : Apex trigger activityCountUpdate caused an unexpected exception, contact your administrator: activityCountUpdate: System.LimitException: Too many query rows: 50001"

Can anyone help me to sort out this issue and update the trigger as well if any thing wrong in it.
Thanks
trigger activityCountUpdate on Task (after insert,after update) {
Set<String> whatIDs = new Set<String>();
    for (Task t : Trigger.new) {
            whatIDs.add(t.whatID);
    }
    Date dt;
    List<aggregateResult> resultsOutbound =[select count(Id) TotalOutbound from Task where WhatId IN :whatIDs and Type in ('Email', 'Email - AM', 'Voicemail', 'Outbound Call', 'Demo', 'CS Call', 'Marketing Email', 'Call')];
	List<aggregateResult> resultsInbound =[select count(Id) TotalInbound from Task where WhatId IN :whatIDs and Type in ('Inbound Call','Save Call')];
    List<aggregateResult> resultsOther =[select count(Id) TotalOther from Task where WhatId IN :whatIDs and Type in ('Other','Assisted Sale','Bad Number','Billing','Live Chat','Meeting','Implementation','')];
    List<aggregateResult> resultsTotal =[select count(Id) Total from Task where WhatId IN :whatIDs];
    
    List<Task> taskData = [select LastModifiedDate from Task where WhatId IN :whatIDs order by LastModifiedDate desc limit 1];
    String d,outbound,inbound,other,total;
    for(Task t : taskData){
        dt = date.parse(t.LastModifiedDate.format('MM/dd/yyyy'));
    }
    for(AggregateResult ar : resultsOutbound){ 
            outbound = String.valueOf(ar.get('TotalOutbound'));
    }
    for(AggregateResult ar1 : resultsInbound){ 
             inbound = String.valueOf(ar1.get('TotalInbound'));
    }
    for(AggregateResult ar2 : resultsOther){ 
             other = String.valueOf(ar2.get('TotalOther'));
    }
	for(AggregateResult ar3 : resultsTotal){ 
            total = String.valueOf(ar3.get('Total'));
    }
    
    List<Opportunity> oppResult = [select Id,Activity_Count_Inbound__c,Activity_Count_Outbound__c,Activity_Count_By_Type__c, Total_Activity_Count__c,Date_of_last_contact__c  from Opportunity where Id =: whatIDs];
  
    for(Opportunity opp : oppResult){
         opp.Activity_Count_Inbound__c =inbound; 
         opp.Activity_Count_Outbound__c = outbound;
         opp.Activity_Count_By_Type__c = other;
         opp.Total_Activity_Count__c = total;
         opp.Date_of_last_contact__c = dt;
    }
    update oppResult;
   
    

}




 
KaranrajKaranraj
I have made couple of changes in the code
  • Added Null check before adding value into the WhatsIds set variable to avoid query to fetch values with null also
  • In the opportunity query instead '=' operator i have changed into 'IN' operator, because WhatsIds is a collection variable.
Try the updated below code
trigger activityCountUpdate on Task (after insert,after update) {
Set<String> whatIDs = new Set<String>();
    for (Task t : Trigger.new) {
          if(t.whatId != NULL)
            whatIDs.add(t.whatID);
    }
    Date dt;
    List<aggregateResult> resultsOutbound =[select count(Id) TotalOutbound from Task where WhatId IN :whatIDs and Type in ('Email', 'Email - AM', 'Voicemail', 'Outbound Call', 'Demo', 'CS Call', 'Marketing Email', 'Call')];
	List<aggregateResult> resultsInbound =[select count(Id) TotalInbound from Task where WhatId IN :whatIDs and Type in ('Inbound Call','Save Call')];
    List<aggregateResult> resultsOther =[select count(Id) TotalOther from Task where WhatId IN :whatIDs and Type in ('Other','Assisted Sale','Bad Number','Billing','Live Chat','Meeting','Implementation','')];
    List<aggregateResult> resultsTotal =[select count(Id) Total from Task where WhatId IN :whatIDs];
    
    List<Task> taskData = [select LastModifiedDate from Task where WhatId IN :whatIDs order by LastModifiedDate desc limit 1];
    String d,outbound,inbound,other,total;
    for(Task t : taskData){
        dt = date.parse(t.LastModifiedDate.format('MM/dd/yyyy'));
    }
    for(AggregateResult ar : resultsOutbound){ 
            outbound = String.valueOf(ar.get('TotalOutbound'));
    }
    for(AggregateResult ar1 : resultsInbound){ 
             inbound = String.valueOf(ar1.get('TotalInbound'));
    }
    for(AggregateResult ar2 : resultsOther){ 
             other = String.valueOf(ar2.get('TotalOther'));
    }
	for(AggregateResult ar3 : resultsTotal){ 
            total = String.valueOf(ar3.get('Total'));
    }
    
    List<Opportunity> oppResult = [select Id,Activity_Count_Inbound__c,Activity_Count_Outbound__c,Activity_Count_By_Type__c, Total_Activity_Count__c,Date_of_last_contact__c  from Opportunity where Id IN: whatIDs];
  
    for(Opportunity opp : oppResult){
         opp.Activity_Count_Inbound__c =inbound; 
         opp.Activity_Count_Outbound__c = outbound;
         opp.Activity_Count_By_Type__c = other;
         opp.Total_Activity_Count__c = total;
         opp.Date_of_last_contact__c = dt;
    }
    update oppResult;
   
}

 
Mayank.msMayank.ms
Thanks Karanraj, I have updated these changes and looking if this exception will come again.