+ Start a Discussion
Niraj Kumar 9Niraj Kumar 9 

Need to be modify the trigger to count the task/event on Contact, Account, opportunity and show the total task/event on Contact, opportunity but on Account It should be sum of all threeObjects task/Event

Hi Guys,
I need to be modify the trigger and apex class so that it will work for Account Count= Account+Contact+Opportunity(task and event).Only for Account , Rests are ok.
trigger UpdateAccountContactopportunityCounttask on task (after insert, after delete) {
      
   
     Set <Id> AccountIDs = new Set <Id> ();
     Set <Id> ContactIDs = new Set <Id> ();
     Set <Id> OpportunityIDs = 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(tsk1.WhoId <> NULL && tsk1.WhoId.getSobjectType() == Contact.getSObjectType()){
              ContactIDs.add(tsk1.WhoId);
          }
         if(tsk1.WhatId <> NULL && tsk1.WhatId.getSobjectType() == opportunity.getSObjectType()){
              OpportunityIDs.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(tsk1.WhoId <> NULL && tsk1.WhoId.getSobjectType() == Contact.getSObjectType()){
              ContactIDs.add(tsk1.WhoId);
          } 
          if(tsk1.WhatId <> NULL && tsk1.WhatId.getSobjectType() == opportunity.getSObjectType()){
              OpportunityIDs.add(tsk1.WhatId);
          } 
      }  
  }
  if(AccountIDs.size()>0)
      ActivityTriggerHandler.UpdateActivityAccount(AccountIDs);
  
  // Recompute the Activities for Account
  
    if(ContactIDs.size()>0)
      ActivityTriggerHandler.UpdateActivityContact(ContactIDs);
  
  // Recompute the Activities for Contact
  
   if(OpportunityIDs.size()>0)
      ActivityTriggerHandler.UpdateActivityOpportunity(OpportunityIDs);
  
  // Recompute the Activities for opportunity
  
}
trigger UpdateAccountContactOpportunityCountevent on event (after insert, after delete) {

  Set <Id> ContactIDs = new Set <Id> ();
   Set <Id> AccountIDs = new Set <Id> ();
    Set <Id> opportunityIDs = new Set <Id> ();
  
  if(Trigger.isInsert ){
      for(Event ev1: Trigger.new){
          if(ev1.WhoId <> NULL && ev1.WhoId.getSobjectType() == Contact.getSObjectType()){
              ContactIDs.add(ev1.WhoId);
          }
          if(ev1.WhatId <> NULL && ev1.WhatId.getSobjectType() == Account.getSObjectType()){
              AccountIDs .add(ev1.WhatId);
          }
        if(ev1.WhatId <> NULL && ev1.WhatId.getSobjectType() == Opportunity.getSObjectType()){
              opportunityIDs .add(ev1.WhatId);
          }  
      }
  }
  if(Trigger.isDelete ){
      for(Event ev1: Trigger.old){
          if(ev1.WhoId <> NULL && ev1.WhoId.getSobjectType() == Contact.getSObjectType()){
              ContactIDs.add(ev1.WhoId);
          }
          if(ev1.WhatId <> NULL && ev1.WhatId.getSobjectType() == Account.getSObjectType()){
              AccountIDs .add(ev1.WhatId);
          }
          if(ev1.WhatId <> NULL && ev1.WhatId.getSobjectType() == Opportunity.getSObjectType()){
              opportunityIDs .add(ev1.WhatId);
          }
      }  
  }
  if(ContactIDs.size()>0)
      ActivityTriggerHandler.UpdateActivityContact(ContactIDs);
  
  // Recompute the Activities for Contact
  
  if(AccountIDs.size()>0)
     
      ActivityTriggerHandler.UpdateActivityAccount(AccountIDs);
  // Recompute the Activities for Account
  
   if(opportunityIDs.size()>0)
      
      ActivityTriggerHandler.UpdateActivityOpportunity(opportunityIDs);
  // Recompute the Activities for opportunity
  
}
Apex classes::::
 
Public class ActivityTriggerHandler{




@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 id from task where Id IN : AccountIDs]){
                 count = AccountCntMap .get(tsk1.WhatId) + 1;
                 AccountCntMap .put(tsk1.WhatId, count);
             }
             for(Event ev1: [select WhatId, Id from Event where Id 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 );
         }
          
 
        
          @future
         public static void UpdateActivityContact(set<Id> ContactIDs){
            MAP <Id,integer> ContactCntMap = new MAP <Id, integer>(); 
            integer count;
             for(ID id1: ContactIDs){
                ContactCntMap.put(id1, 0);  
             }
             for(Task tsk1: [select WhoId, Id from Task where WhoId IN :ContactIDs]){
                 count = ContactCntMap.get(tsk1.WhoId) + 1;
                 ContactCntMap.put(tsk1.WhoId, count);
             }
             for(Event ev1: [select WhoId, Id from Event where WhoId IN :ContactIDs]){
                 count = ContactCntMap.get(ev1.WhoId) + 1;
                 ContactCntMap.put(ev1.WhoId, count);
             }
             LIST <Contact> ContUpd = new LIST <Contact>();
             for(Contact cont1: [SELECT Id, Count_of_Activity__c FROM Contact WHERE Id IN :ContactIDs]){
                count = ContactCntMap.get(cont1.Id);
                cont1.Count_of_Activity__c = count;
                 ContUpd.add(cont1);
             }
             Database.update(ContUpd);
           }
   
         
         @future
       
         public static void UpdateActivityOpportunity(set<Id> opportunityIDs ){
            MAP <Id,integer> OpportunityCntMap = new MAP <Id, integer>(); 
            integer count;
             for(ID id1: opportunityIDs ){
                OpportunityCntMap .put(id1, 0);  
             }
             for(Task tsk1: [select WhatId, Id from Task where WhatId IN :opportunityIDs ]){
                 count = OpportunityCntMap.get(tsk1.WhatId) + 1;
                 OpportunityCntMap.put(tsk1.WhatId, count);
             }
             for(Event ev1: [select WhatId, Id from Event where WhatId IN :opportunityIDs ]){
                 count = OpportunityCntMap.get(ev1.WhatId) + 1;
                 OpportunityCntMap.put(ev1.WhatId, count);
             }
             LIST <Opportunity> OppUpd = new LIST <Opportunity>();
             for(opportunity Opp1: [SELECT Id, Count_of_Activity__c FROM Opportunity WHERE Id IN :opportunityIDs ]){
                count = OpportunityCntMap.get(Opp1.Id);
                Opp1.Count_of_Activity__c = count;
                 OppUpd.add(Opp1);
             }
             Database.update(OppUpd);
         }  
      
       }
Thanks in advance.

Regards,
Niraj Kumar.