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
Efstathios Ntatsios 6Efstathios Ntatsios 6 

Trigger issues

I used the following apex trigger in order to count the amount of open and closed tasks on opportunities.

trigger CountTask on Task (after insert, after update) {
    
    public List<Task> ltask1 = new List<Task>();
    public id oppid;
    public integer inp=0;   
    public integer inr=0;
    for(Task t:Trigger.New){
        oppid = t.WhatId;
        system.debug('oppid'+oppid);
        
    }
    ltask1 = [select id,Status from task where whatid=:oppid];
    system.debug('oppsize'+ltask1.size());
    
    
    for(task t:ltask1){
        if(t.Status!='Abgeschlossen'){
            inp = inp+1;
        } else{
            inr = inr +1;
        }                
    }
    List<Opportunity> opp = new List<opportunity>();    
    List<opportunity> op = [select id from Opportunity where id = :oppid];
    system.debug('oppsize'+op.size());
    for(opportunity o: op){
        o.Open_Tasks__c = inp;
        o.Closed_Tasks__c = inr;
        opp.add(o);  
    }
    if(opp.size()>0){
        update opp;
    }        
    system.debug('No of Tasks'+inp+inr);       
}

when trying to create a task on an opportunity I get the the following error:

CountTask: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 0065800000MKq82AAD; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: [] Trigger.CountTask: line 32, column 1

Since I am a bit new with Apex triggers, could someone explain, what do I need to do to fix this?
Thank you very much in advance

Debug Log:
30.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WAVE,INFO;WORKFLOW,INFO
16:28:44.2 (2103082)|ENTERING_MANAGED_PKG|APXTConga4
16:28:44.2 (11606583)|SOQL_EXECUTE_BEGIN|[46]|Aggregations:0|SELECT id, APXTConga4__WhatId__c FROM APXTConga4__Conga_Email_Staging__c 
16:28:44.2 (17655073)|SOQL_EXECUTE_END|[46]|Rows:0
16:28:44.18 (18693349)|CUMULATIVE_LIMIT_USAGE
16:28:44.18 (18693349)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

16:28:44.18 (18693349)|LIMIT_USAGE_FOR_NS|APXTConga4|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

16:28:44.18 (18693349)|CUMULATIVE_LIMIT_USAGE_END

16:28:44.166 (166843648)|CODE_UNIT_STARTED|[EXTERNAL]|01q3E000000CzBl|Counttasks on Task trigger event AfterInsert for [00T3E000006N4P7]
16:28:44.166 (166926635)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
16:28:44.166 (167097773)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:10
16:28:44.166 (167215648)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:28
16:28:44.166 (167237693)|VARIABLE_SCOPE_BEGIN|[1]|this|Counttasks|true|false
16:28:44.166 (167316006)|VARIABLE_ASSIGNMENT|[1]|this|{}|0xba5e15e
16:28:44.166 (167401981)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:28
16:28:44.166 (167726154)|VARIABLE_SCOPE_BEGIN|[1]|this|Counttasks|true|false
16:28:44.166 (167769513)|VARIABLE_ASSIGNMENT|[1]|this|{}|0xba5e15e
16:28:44.166 (167782415)|STATEMENT_EXECUTE|[1]
16:28:44.166 (167784464)|STATEMENT_EXECUTE|[3]
16:28:44.166 (167814696)|HEAP_ALLOCATE|[3]|Bytes:4
16:28:44.166 (167882366)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
16:28:44.166 (167897985)|VARIABLE_ASSIGNMENT|[3]|this.ltask1|[]|0xba5e15e
16:28:44.166 (167904053)|STATEMENT_EXECUTE|[4]
16:28:44.166 (167916662)|VARIABLE_ASSIGNMENT|[4]|this.oppid|null|0xba5e15e
16:28:44.166 (167922885)|STATEMENT_EXECUTE|[5]
16:28:44.166 (167930867)|HEAP_ALLOCATE|[5]|Bytes:4
16:28:44.166 (167940385)|VARIABLE_ASSIGNMENT|[5]|this.inp|0|0xba5e15e
16:28:44.166 (167945619)|STATEMENT_EXECUTE|[6]
16:28:44.166 (167951062)|HEAP_ALLOCATE|[6]|Bytes:4
16:28:44.166 (167959200)|VARIABLE_ASSIGNMENT|[6]|this.inr|0|0xba5e15e
16:28:44.166 (168076642)|HEAP_ALLOCATE|[7]|Bytes:5
16:28:44.166 (168163019)|VARIABLE_SCOPE_BEGIN|[7]|t|Task|true|false
16:28:44.166 (183115571)|VARIABLE_ASSIGNMENT|[7]|t|{"LastModifiedDate":"2017-10-23T14:28:44.000Z","AccountId":"0015800000rcsReAAI","WhatId":"0065800000MKq82AAD","WhatCount":1,"IsHighPriority":false,"IsClosed":false,"WhoCount":0,"CreatedById":"00558000001scKvAAI","OwnerId":"00558000001scKvAAI","RecordTypeId":"01258000000cf60AAA","IsReminderSet":false,"CallAttptDateVa__c":"2017-10-23T14:28:44.000Z","Agent_ID__c":"00558000001scKv","Status":"Offen","QualifiedVa__c":false,"IsDeleted":false,"ActivityDate":"2017-10-27T00:00:00.000Z","Priority":"Normal","Qualified__c":false,"CurrencyIsoCode":"EUR","IsRecurrence":false,"Subject":"Test task","SystemModstamp":"2017-10-23T14:28:44.000Z","IsArchived":false,"Overdue_Task__c":"Not yet due","TaskSubtype":"Task","CreatedDate":"2017-10-23T14:28:44.000Z","Id":"00T3E000006N4P7UAK","etreminder__Reminder (3 more) ...":false,"LastModifiedById":"00558000001scKvAAI"}|0x4b02ac77
16:28:44.166 (183156343)|STATEMENT_EXECUTE|[7]
16:28:44.166 (183158555)|STATEMENT_EXECUTE|[8]
16:28:44.166 (183418924)|VARIABLE_ASSIGNMENT|[8]|this.oppid|"0065800000MKq82AAD"|0xba5e15e
16:28:44.166 (183428932)|STATEMENT_EXECUTE|[9]
16:28:44.166 (183436148)|HEAP_ALLOCATE|[9]|Bytes:5
16:28:44.166 (183536527)|HEAP_ALLOCATE|[9]|Bytes:18
16:28:44.166 (183599211)|HEAP_ALLOCATE|[9]|Bytes:23
16:28:44.166 (183637592)|USER_DEBUG|[9]|DEBUG|oppid0065800000MKq82AAD
16:28:44.166 (183674707)|HEAP_ALLOCATE|[7]|Bytes:5
16:28:44.166 (183707930)|VARIABLE_ASSIGNMENT|[7]|t|null|
16:28:44.166 (183720883)|STATEMENT_EXECUTE|[12]
16:28:44.166 (183726941)|HEAP_ALLOCATE|[12]|Bytes:51
16:28:44.166 (183759355)|HEAP_ALLOCATE|[12]|Bytes:4
16:28:44.166 (184248439)|SOQL_EXECUTE_BEGIN|[12]|Aggregations:0|SELECT id, Status FROM task WHERE whatid = :tmpVar1
16:28:44.166 (210054678)|SOQL_EXECUTE_END|[12]|Rows:1
16:28:44.166 (210102431)|HEAP_ALLOCATE|[12]|Bytes:8
16:28:44.166 (210127862)|HEAP_ALLOCATE|[12]|Bytes:44
16:28:44.166 (210190225)|HEAP_ALLOCATE|[12]|Bytes:8
16:28:44.166 (210271650)|VARIABLE_ASSIGNMENT|[12]|this.ltask1|[{"Id":"00T3E000006N4P7UAK","Status":"Offen"}]|0xba5e15e
16:28:44.166 (210283838)|STATEMENT_EXECUTE|[13]
16:28:44.166 (210291150)|HEAP_ALLOCATE|[13]|Bytes:7
16:28:44.166 (210392442)|HEAP_ALLOCATE|[13]|Bytes:1
16:28:44.166 (210413354)|HEAP_ALLOCATE|[13]|Bytes:8
16:28:44.166 (210430785)|USER_DEBUG|[13]|DEBUG|oppsize1
16:28:44.166 (210511107)|HEAP_ALLOCATE|[16]|Bytes:5
16:28:44.166 (210548229)|HEAP_ALLOCATE|[16]|Bytes:12
16:28:44.166 (210568605)|VARIABLE_SCOPE_BEGIN|[16]|t|Task|true|false
16:28:44.166 (210591779)|VARIABLE_ASSIGNMENT|[16]|t|{"Id":"00T3E000006N4P7UAK","Status":"Offen"}|0x1f929a41
16:28:44.166 (210601216)|STATEMENT_EXECUTE|[16]
16:28:44.166 (210637116)|HEAP_ALLOCATE|[17]|Bytes:9
16:28:44.166 (210670079)|STATEMENT_EXECUTE|[17]
16:28:44.166 (210671997)|STATEMENT_EXECUTE|[18]
16:28:44.166 (210686339)|HEAP_ALLOCATE|[18]|Bytes:4
16:28:44.166 (210699455)|VARIABLE_ASSIGNMENT|[18]|this.inp|1|0xba5e15e
16:28:44.166 (210717258)|HEAP_ALLOCATE|[16]|Bytes:5
16:28:44.166 (210733116)|VARIABLE_ASSIGNMENT|[16]|t|null|
16:28:44.166 (210745375)|STATEMENT_EXECUTE|[23]
16:28:44.166 (210778777)|HEAP_ALLOCATE|[23]|Bytes:4
16:28:44.166 (210859430)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
16:28:44.166 (210872795)|VARIABLE_ASSIGNMENT|[23]|this.opp|[]|0xba5e15e
16:28:44.166 (210879797)|STATEMENT_EXECUTE|[24]
16:28:44.166 (210883638)|HEAP_ALLOCATE|[24]|Bytes:46
16:28:44.166 (210901808)|HEAP_ALLOCATE|[24]|Bytes:4
16:28:44.166 (211225629)|SOQL_EXECUTE_BEGIN|[24]|Aggregations:0|SELECT id FROM Opportunity WHERE id = :tmpVar1
16:28:44.166 (228382474)|SOQL_EXECUTE_END|[24]|Rows:1
16:28:44.166 (228423557)|HEAP_ALLOCATE|[24]|Bytes:8
16:28:44.166 (228445757)|HEAP_ALLOCATE|[24]|Bytes:29
16:28:44.166 (228509663)|HEAP_ALLOCATE|[24]|Bytes:8
16:28:44.166 (228590026)|VARIABLE_ASSIGNMENT|[24]|this.op|[{"Id":"0065800000MKq82AAD"}]|0xba5e15e
16:28:44.166 (228602917)|STATEMENT_EXECUTE|[25]
16:28:44.166 (228699301)|HEAP_ALLOCATE|[25]|Bytes:1
16:28:44.166 (228720486)|HEAP_ALLOCATE|[25]|Bytes:8
16:28:44.166 (228757069)|USER_DEBUG|[25]|DEBUG|oppsize1
16:28:44.166 (228950342)|HEAP_ALLOCATE|[26]|Bytes:5
16:28:44.166 (228986680)|HEAP_ALLOCATE|[26]|Bytes:8
16:28:44.166 (229004080)|VARIABLE_SCOPE_BEGIN|[26]|o|Opportunity|true|false
16:28:44.166 (229024855)|VARIABLE_ASSIGNMENT|[26]|o|{"Id":"0065800000MKq82AAD"}|0x3cd4bd33
16:28:44.166 (229032667)|STATEMENT_EXECUTE|[26]
16:28:44.166 (229034029)|STATEMENT_EXECUTE|[27]
16:28:44.166 (229080362)|HEAP_ALLOCATE|[27]|Bytes:28
16:28:44.166 (229087921)|HEAP_ALLOCATE|[27]|Bytes:28
16:28:44.166 (229148700)|HEAP_ALLOCATE|[27]|Bytes:-4
16:28:44.166 (229165722)|VARIABLE_ASSIGNMENT|[27]|this.Open_Tasks__c|1|0x3cd4bd33
16:28:44.166 (229171858)|STATEMENT_EXECUTE|[28]
16:28:44.166 (229190966)|HEAP_ALLOCATE|[28]|Bytes:28
16:28:44.166 (229197103)|HEAP_ALLOCATE|[28]|Bytes:28
16:28:44.166 (229229141)|HEAP_ALLOCATE|[28]|Bytes:-4
16:28:44.166 (229240584)|VARIABLE_ASSIGNMENT|[28]|this.Closed_Tasks__c|0|0x3cd4bd33
16:28:44.166 (229246898)|STATEMENT_EXECUTE|[29]
16:28:44.166 (229295763)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
16:28:44.166 (229331151)|HEAP_ALLOCATE|[26]|Bytes:5
16:28:44.166 (229351385)|VARIABLE_ASSIGNMENT|[26]|o|null|
16:28:44.166 (229392780)|STATEMENT_EXECUTE|[31]
16:28:44.166 (229396733)|STATEMENT_EXECUTE|[32]
16:28:44.166 (229472763)|DML_BEGIN|[32]|Op:Update|Type:Opportunity|Rows:1
16:28:44.166 (229520965)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
16:28:44.320 (320138761)|ENTERING_MANAGED_PKG|Five9LSP
16:28:44.320 (325816235)|ENTERING_MANAGED_PKG|Five9LSP
16:28:44.320 (325879741)|ENTERING_MANAGED_PKG|Five9LSP
16:28:44.320 (326061566)|ENTERING_MANAGED_PKG|Five9LSP
16:28:44.332 (332314872)|CUMULATIVE_LIMIT_USAGE
16:28:44.332 (332314872)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 2 out of 100
  Number of query rows: 2 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

16:28:44.332 (332314872)|LIMIT_USAGE_FOR_NS|APXTConga4|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

16:28:44.332 (332314872)|CUMULATIVE_LIMIT_USAGE_END

16:28:44.166 (337368997)|CODE_UNIT_STARTED|[EXTERNAL]|01q58000000eNwB|OpportunityTrigger on Opportunity trigger event BeforeUpdate for [0065800000MKq82]
16:28:44.166 (337404714)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
16:28:44.166 (337413925)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
16:28:44.166 (337458882)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
16:28:44.166 (337472388)|VARIABLE_SCOPE_BEGIN|[1]|this|OpportunityTrigger|true|false
16:28:44.166 (337540283)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x79af66c3
16:28:44.166 (337586081)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
16:28:44.166 (337598233)|VARIABLE_SCOPE_BEGIN|[1]|this|OpportunityTrigger|true|false
16:28:44.166 (337614878)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x79af66c3
16:28:44.166 (337623491)|STATEMENT_EXECUTE|[1]
16:28:44.166 (337637315)|STATEMENT_EXECUTE|[3]
16:28:44.166 (337646669)|STATEMENT_EXECUTE|[6]
16:28:44.337 (337650964)|CUMULATIVE_LIMIT_USAGE
16:28:44.337 (337650964)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 2 out of 100
  Number of query rows: 2 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

16:28:44.337 (337650964)|LIMIT_USAGE_FOR_NS|APXTConga4|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

16:28:44.337 (337650964)|CUMULATIVE_LIMIT_USAGE_END

16:28:44.166 (339259338)|CODE_UNIT_FINISHED|OpportunityTrigger on Opportunity trigger event BeforeUpdate for [0065800000MKq82]
16:28:44.166 (389961988)|DML_END|[32]
16:28:44.166 (390113032)|EXCEPTION_THROWN|[32]|System.DmlException: Update failed. First exception on row 0 with id 0065800000MKq82AAD; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []
16:28:44.166 (390843952)|HEAP_ALLOCATE|[32]|Bytes:144
16:28:44.166 (391119104)|FATAL_ERROR|System.DmlException: Update failed. First exception on row 0 with id 0065800000MKq82AAD; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

Trigger.Counttasks: line 32, column 1
16:28:44.166 (391137831)|FATAL_ERROR|System.DmlException: Update failed. First exception on row 0 with id 0065800000MKq82AAD; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []

Trigger.Counttasks: line 32, column 1
16:28:44.391 (391143142)|CUMULATIVE_LIMIT_USAGE
16:28:44.391 (391143142)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 2 out of 100
  Number of query rows: 2 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

16:28:44.391 (391143142)|LIMIT_USAGE_FOR_NS|APXTConga4|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

16:28:44.391 (391143142)|CUMULATIVE_LIMIT_USAGE_END
Wilfredo Morillo 20Wilfredo Morillo 20
You need to filter the tasks just when is related to an opportunity try this: 
trigger CountTask on Task ( after insert,after update) {
    
	set<string> oppIds =  new set<String>();
    
    for(task oppTask :trigger.new){
        string taskWhatid = oppTask.whatid;
        
        if( taskWhatid.left(3) == '006'){
            oppIds.add(taskWhatid);
        }  
    }
   
    map<id,Opportunity> taskOpps = new map<id,Opportunity>([Select id from opportunity where id in:oppIds]);
    
    for(AggregateResult ar: [Select whatid whatId,count(id) openTasks from task where whatid in :oppIds and Status!='Abgeschlossen' group by whatid]){
       Opportunity tempOpp = taskOpps.get((id)ar.get('Whatid'));
       
        tempOpp.Open_Tasks__c = valueOf((integer)ar.get('openTasks'));
        taskOpps.put(tempOpp.id,tempOpp);
    }
    
    for(AggregateResult ar: [Select whatid whatId,count(id) openTasks from task where whatid in :oppIds and Status='Abgeschlossen' group by whatid]){
       Opportunity tempOpp = taskOpps.get((id)ar.get('Whatid'));
       
        tempOpp.Closed_Tasks__c = valueOf((integer)ar.get('openTasks'));
        taskOpps.put(tempOpp.id,tempOpp);
    }
    
    Update taskOpps.values();
       
}

If it help please mak as best.  
 
Efstathios Ntatsios 6Efstathios Ntatsios 6

Hello Wilfredo,

Thank you very much for your reply. I managed to locate why I was getting this error but it actually did not had to anything the trigger itself. That said I went ahead and deployed the trigger in our production environment today only to face a much more important issue, that did not happen in my partial copy sandbox. I ran into the following error:

Apex script unhandled trigger exception by user/organization: 00558000001txwG/00D58000000bN6p

CountTask: execution of AfterInsert

caused by: System.QueryException: Non-selective query against large object type (more than 200000 rows). Consider an indexed filter or contact salesforce.com about custom indexing.
Even if a field is indexed a filter might still not be selective when:
1. The filter value includes null (for instance binding with a list that contains null)
2. Data skew exists whereby the number of matching rows is very large (for instance, filtering for a particular foreign key value that occurs many times)

Trigger.CountTask: line 12, column 1

I am not familiar with this error but do you think that using the trigger you have written will solve this? I am almost certain that the issue lies at scenario no.2 since we use an 3rd party application that should be causing this.

Wilfredo Morillo 20Wilfredo Morillo 20
Give the trigger I wrote a try. My trigger is limiting the return to only opportunity tasks and related to the opportunities in trigger.new. 

Let me know if you have any trouble.