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
Chad MoutesChad Moutes 

Apex Trigger to Count Tasks Not Working

I have an Apex Trigger that I am creating that is to count the number of Tasks that are on an Account, the code is listed below.
 
trigger SumTotalActivitesOnAccount on Task (after insert, after update, after delete) {

    set<Id> set_Id = new set<Id>();
    
    List<Account>acc_list = new List<Account>();
    
    if(Trigger.isInsert || Trigger.isUpdate) {
        for(Task T:trigger.new){
            set_Id.add(T.What);
        }
        
    }
    else if(Trigger.isDelete){
        for(Task T:Trigger.old){
            set_Id.add(T.What);
        }
        
    }
    
    if(Trigger.isAfter && (Trigger.isUpdate || Trigger.isInsert || Trigger.isDelete)){
        acc_list=[SELECT Id, Sum_Total_Activities__c, (SELECT Id, FROM Tasks) FROM Account WHERE Id IN :set_Id];
    
    for(Account acc: acc_list){
        if(acc.Tasks.size()>0)
            acc.Sum_Total_Activities__c = acc.Tasks.size();
        else
            acc.Sum_Total_Activities__c = 0;
    }
     if(!acc_list.isEmpty())
            update acc_list;
  }

}

I keep getting an error that states: Compile Error: unexpected token: 'FROM' at line 21 column 66

Any help would be greatly appreciated.
Best Answer chosen by Chad Moutes
BalajiRanganathanBalajiRanganathan
you need to update set_Id.add(T.What.Id); to 
set_Id.add(T.WhatId);

also, add the whatid to set only if it is account id by checking.

 if  ((T.WhatId + '').startsWith('001'))

All Answers

BalajiRanganathanBalajiRanganathan
You have unnecessary comma (,) in your SOQL
(SELECT Id, FROM Tasks) this should be replaced to 

(SELECT Id FROM Tasks)
Chad MoutesChad Moutes
Alright, I made that change. Now I'm getting an error  Incompatible element type SOBJECT:Name for collection of Id at line 9 column 13. Any ideas?
Chad MoutesChad Moutes
Alright, so here is my updated code, I receive no errors it saved fine, but it is not working. It is simply supposed to count the number of tasks that are assigned to an account.
 
trigger SumTotalActivitesOnAccount on Task (after insert, after update, after delete) {

    set<Id> set_Id = new set<Id>();
   
    List<Account>acc_list = new List<Account>();
    
    if(Trigger.isInsert || Trigger.isUpdate) {
        for(Task T:trigger.new){
            set_Id.add(T.What.Id);
        }

     }
    else if(Trigger.isDelete){
        for(Task T:Trigger.old){
            set_Id.add(T.What.Id);
        }

     }
     
    if(Trigger.isAfter && (Trigger.isUpdate || Trigger.isInsert || Trigger.isDelete)){
        acc_list=[SELECT Id, Sum_Total_Activities__c, (SELECT Id FROM Tasks) FROM Account WHERE Id IN :set_Id];

     for(Account acc: acc_list){
        if(acc.Tasks.size()>0)
            acc.Sum_Total_Activities__c = acc.Tasks.size();
        else
            acc.Sum_Total_Activities__c = 0;
     }
        if(!acc_list.isEmpty())
            update acc_list;
        }

}

Please help lol
BalajiRanganathanBalajiRanganathan
you need to update set_Id.add(T.What.Id); to 
set_Id.add(T.WhatId);

also, add the whatid to set only if it is account id by checking.

 if  ((T.WhatId + '').startsWith('001'))
This was selected as the best answer
Chad MoutesChad Moutes
Thank you so much!