You need to sign in to do that
Don't have an account?
Mayank.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.
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 50000Unable 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; } } }
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!
Update code on sandbox but still getting limit warning.
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!