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 

Trigger on Task (Apex governor limit warning email recived sometimes)

Hi,

I have created a trigger on task for after insert and after update. That updates the opporutnity custom fields based on task 'type'  counts. It is working but i have also reciving Apex governor limit warnig. This waring is coming after push this trigger on production SF. Can anybody please correct my trigger to avoid these waring issue.

Thanks in advance.
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 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 opp;
    }
   
    

}

 
RAM AnisettiRAM Anisetti
can you post warning message once.
Mayank.msMayank.ms
Warning emails like as below:

Operation: insert.Task

By user/organization: 005160000064dkR/00DG0000000k8tZ

Caused the following Apex resource warnings:

Number of query rows: 25102 out of 50000

-------------------------------------------------------------

Operation: insert.Name

By user/organization: 005160000064dkR/00DG0000000k8tZ

Caused the following Apex resource warnings:

Number of query rows: 25137 out of 50000

---------------------------------------------------------------------------

Operation: insert.Name

By user/organization: 005160000064dkR/00DG0000000k8tZ

Caused the following Apex resource warnings:

Number of query rows: 25183 out of 50000

(these emails can be disabled from the user detail page for this user)
 
Vishal_GuptaVishal_Gupta
Hi Kamran,

After analysing your code, I am assuming that there are multiple triggers on Task or somehow there are more code executing including this trigger in same context because in this trigger there is only one query which is fetching more then one record which is List<Opportunity> oppResult = [select Id from Opportunity where Id =: whatIDs];

Or there is bulk of tasks inserting with lots of opportunity whatIds.

Anyways mails which you recived are only warnings and not any issue in your code, you can disabled these mail from your user detail page in My Email Settings -->Disable Send Apex Warning Emails checkbox

Please let me know if need more input.

Thanks,
Vishal
Mayank.msMayank.ms
Thanks Vishal, But can you please make sure this trigger and warning not effecting other triggers execution.
Vishal_GuptaVishal_Gupta
Hi Kamran,

There is only one flow in your trigger code that is DML in for loop, just change last few lines of your code like this :
 
List<Opportunity> oppResult = [select Id,Activity_Count_Inbound__c,Activity_Count_Inbound__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_Inbound__c = outbound;
opp.Activity_Count_By_Type__c = other;
opp.Total_Activity_Count__c = total;
opp.Date_of_last_contact__c = dt;
 }
update oppResult;

It will make your trigger more effecient and there is nothing wrong in it which will impact any other code.

Thanks,
Vishal 
Mayank.msMayank.ms
Thanks a lot Vishal.
Mayank.msMayank.ms

Today,I got the mail. 

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

Whats the issue is going on with this trigger.

Thanks