+ Start a Discussion
Niraj Kumar 9Niraj Kumar 9 

Old value is not counting in task trigger

Hi,
Hhere is the task trigger and class for counting count of activity.Old task of existing Account is not counting .

Here is code.
 
trigger AccountCounttask on task (after insert, after delete) {

  Set <Id> AccountIDs = new Set <Id> ();
  
  if(Trigger.isInsert ){
      for(Task tsk1: Trigger.new){
          if(tsk1.WhatId <> NULL && tsk1.WhatId.getSobjectType() == Account.getSObjectType()){
              AccountIDs .add(tsk1.WhatId);
          }
      }
  }
  if(Trigger.isDelete ){
      for(Task tsk1: Trigger.old){
         if(tsk1.WhatId <> NULL && tsk1.WhatId.getSobjectType() == Account.getSObjectType()){
              AccountIDs .add(tsk1.WhatId);
          }
      }  
  }
  if(AccountIDs.size()>0)
      ActivityTriggerHandler.UpdateActivityAccount(AccountIDs);
  
  // Recompute the Activities for Account
}
@future
       
         public static void UpdateActivityAccount(set<Id> AccountIDs ){
            MAP <Id,integer> AccountCntMap = new MAP <Id, integer>(); 
            integer count;
             for(ID id1: AccountIDs ){
                AccountCntMap .put(id1, 0);  
             }
             for(Task tsk1: [select WhatId, Id from Task where WhatId IN :AccountIDs ]){
                 count = AccountCntMap .get(tsk1.WhatId) + 1;
                 AccountCntMap .put(tsk1.WhatId, count);
             }
             for(Event ev1: [select WhatId, Id from Event where WhatId IN :AccountIDs ]){
                 count = AccountCntMap .get(ev1.WhatId) + 1;
                 AccountCntMap .put(ev1.WhatId, count);
             }
             LIST <Account> AccUpd = new LIST <Account>();
             for(Account Acc1: [SELECT Id, Count_of_Activity__c FROM Account WHERE Id IN :AccountIDs ]){
                count = AccountCntMap .get(Acc1.Id);
                Acc1.Count_of_Activity__c = count;
                 AccUpd .add(Acc1);
             }
             Database.update(AccUpd );
         }

Thanks in Advance.

 
ManojjenaManojjena
HI Niraj,
Modify your query like below it will work !!
[SELECT WhatId, Id from Task where WhatId IN :AccountIDs AND isDeleted=false ALL ROWS]
Let me know if it helps !!
Thanks
Manoj
 
Niraj Kumar 9Niraj Kumar 9
Hi Manoj,
NO, With this solution its not working.

Thanks
Niraj