I have a trigger on task to count open tasks on opportunity. It is working fine when we create single task at a time but when I create log a call and follow up task on same time it gives me error. Please find below screen shots:
Below is my trigger code:
trigger OpenTaskCount on Task (after insert, after update, after delete, after undelete) {
List<Task> taskList = (Trigger.isInsert|| Trigger.isUnDelete) ? Trigger.new : Trigger.old;
List<Id> taskIds = new List<Id>();
for (Task tsk : taskList) {
taskIds.add(tsk.WhatID);
}
List<Opportunity> oppList = [
select
id,
(select id, WhatID, IsClosed from Tasks),
Open_Tasks__c,Total_Tasks__c
from
Opportunity
where
id in :taskIds];
for (Opportunity opp : oppList) {
Integer count = 0;
for(Task tsk : opp.Tasks)
{
if(tsk.WhatId != null && !tsk.IsClosed)
count += 1;
}
opp.Total_Tasks__c = opp.Tasks.size();
opp.Open_Tasks__c = count;
}
update oppList;
}
Please help me how can I get rid of this issue.