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 

Apex governor limit warning: when active task trigger

Hi, 
Getting Apex governor limit warning when we active on task trigger. Can you guys please help on it.
Thanks in advance.
Operation: insert.Name

By user/organization: 005160000064dkR/00DG0000000k8tZ

Caused the following Apex resource warnings:

Number of query rows: 25477 out of 50000
Unable to uderstands whats the issue with the trigger.
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,Accounting_Platform__c,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;
        if(opp.Accounting_Platform__c!=''){
            update opp;
        }
        
    }
    
  
   
}



 
Patcs_1Patcs_1

Hi

Create a list for opportunity and add the Opp in to the list and update the list outside of the for loop.

like below,

List<Opportunity> listopp = new List<opportunity>();

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;
        if(opp.Accounting_Platform__c!=''){
           listopp.add(opp)
        }
}

if(listopp.size() > 0)
{
    update listopp;
}



Hope this helps!

Thanks!
Mayank.msMayank.ms
Thanks Patcs_1,

Update code on sandbox but still getting limit warning.
 
Operation: insert.Contact

By user/organization: 005160000064dkR/00Dq0000000Avtq

Caused the following Apex resource warnings:

Number of SOQL queries: 52 out of 100

 
Patcs_1Patcs_1
Hi

Try to combine the Query in to one and use if statement to add them into the list. Use Group by in aggregrate function.

Thanks!