+ Start a Discussion
Niraj Kumar 9Niraj Kumar 9 

Trigger is not counting closed activity

Hi,

Below is my trigger, its not counting the closed  Activity.
Need your help.
 
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 );
         }

 
Best Answer chosen by Niraj Kumar 9
Srinivas SSrinivas S
trigger AccountCounttask on task (after insert, after delete, after delete) {

  Set <Id> AccountIDs = new Set <Id>();  
  for(Task tsk1: trigger.isDelete ? trigger.old : trigger.new){
	  if(tsk1.WhatId <> NULL && tsk1.WhatId.getSobjectType() == Account.getSObjectType()){
		  AccountIDs .add(tsk1.WhatId);
	  }
  }
  if(AccountIDs.size()>0)
      ActivityTriggerHandler.UpdateActivityAccount(AccountIDs);
}
 
@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 ]){
		 if(AccountCntMap.containsKey(tsk1.whatId))
			AccountCntMap.get(tsk1.WhatId) += AccountCntMap .get(tsk1.WhatId) + 1;
		 else
			AccountCntMap .put(tsk1.WhatId, 0);
	 }
	 for(Event ev1: [select WhatId, Id from Event where WhatId IN :AccountIDs ]){
		 if(AccountCntMap.containsKey(ev1.whatId))
			AccountCntMap.get(ev1.WhatId) += AccountCntMap.get(ev1.WhatId) + 1;
		 else
			AccountCntMap.put(ev1.WhatId, count);
	 }
	 LIST <Account> AccUpdLst = new LIST <Account>();
	 for(Id accId: AccountCntMap.keyset()){
		 AccUpdLst.add(new Account(Id = accId, Count_of_Activity__c = AccountCntMap.get(accId)));
	 }
	 if(AccUpdLst.size() > 0) {
		update AccUpdLst;
	 }
 }

Thanks,
Srinivas
- Please mark as solution if your problem is resolved.

All Answers

sandeep reddy 37sandeep reddy 37
in trigger define one more collection type for deleting records 
and pass the parameter in future method then u do
Srinivas SSrinivas S
trigger AccountCounttask on task (after insert, after delete, after delete) {

  Set <Id> AccountIDs = new Set <Id>();  
  for(Task tsk1: trigger.isDelete ? trigger.old : trigger.new){
	  if(tsk1.WhatId <> NULL && tsk1.WhatId.getSobjectType() == Account.getSObjectType()){
		  AccountIDs .add(tsk1.WhatId);
	  }
  }
  if(AccountIDs.size()>0)
      ActivityTriggerHandler.UpdateActivityAccount(AccountIDs);
}
 
@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 ]){
		 if(AccountCntMap.containsKey(tsk1.whatId))
			AccountCntMap.get(tsk1.WhatId) += AccountCntMap .get(tsk1.WhatId) + 1;
		 else
			AccountCntMap .put(tsk1.WhatId, 0);
	 }
	 for(Event ev1: [select WhatId, Id from Event where WhatId IN :AccountIDs ]){
		 if(AccountCntMap.containsKey(ev1.whatId))
			AccountCntMap.get(ev1.WhatId) += AccountCntMap.get(ev1.WhatId) + 1;
		 else
			AccountCntMap.put(ev1.WhatId, count);
	 }
	 LIST <Account> AccUpdLst = new LIST <Account>();
	 for(Id accId: AccountCntMap.keyset()){
		 AccUpdLst.add(new Account(Id = accId, Count_of_Activity__c = AccountCntMap.get(accId)));
	 }
	 if(AccUpdLst.size() > 0) {
		update AccUpdLst;
	 }
 }

Thanks,
Srinivas
- Please mark as solution if your problem is resolved.
This was selected as the best answer