+ Start a Discussion
Nabeel Munir 9Nabeel Munir 9 

update field in account with the count of open tasks and events.

Hello everyone,
I am trying to update the field in account object. The field is called, open_activities__c. The count of all open activities for the corresponding account should appear on the field. 
I am getting the right count of activities(Tasks,Events) But the corresponding account Id is not being stored in my map datastructure. I have variable from aggregate query called cnt, that stores the count of open tasks/events. and accId that gets the WhatId of those tasks and events and I am putting both variables into my map<Id,Double> 
Here is the log result.

Log result for cnt:
14:42:44:055 VARIABLE_ASSIGNMENT [6]|agg|{"cnt":9}|0x6729bfa8
Log result for accId:
14:42:44:055 VARIABLE_SCOPE_BEGIN [7]|accId|Id|false|false
14:42:44:055 VARIABLE_ASSIGNMENT [7]|accId|null
I can not figure out the reason for the accId to be null.
Here is the code and trigger:
public class ActivityCounter {

   public static void activityCount(Set<Id> accountIds) { 
  
   List<Account> acctToUpdate = new List<Account>();
    Integer taskcounts=0;
    System.debug('coming here atleast');
   Map<Id,Double> tasksByAccount = new Map<Id, Double>();
   for (AggregateResult agg : [Select WhatId , count(Id) cnt From Task Where WhatId IN :accountIds and IsClosed = false GROUP BY WhatId]) {
     Id accId = (Id) agg.get('WhatId');
     Double cnt = (Double) agg.get('cnt');
     taskcounts=cnt.intValue();
     System.debug('Tasks got: '+taskcounts);  
     tasksByAccount.put(accId, cnt);
   }
    System.debug('size of tasks by account: '+tasksByAccount.size());
   Map<Id, Double> eventsByAccount = new Map<Id, Double>();
   for(AggregateResult agg : [Select  WhatId , count(Id) cnt From Event Where WhatId IN :accountIds and  EndDateTime <= TODAY GROUP BY WhatId]) {
     Id accId = (Id) agg.get('WhatId');
     Double cnt = (Double) agg.get('cnt');
         
     eventsByAccount.put(accId, cnt);
   }
    System.debug('Reaches events'); 
   for(Id accountId : accountIds) {
     Double taskCount = tasksByAccount.containsKey(accountId) ? tasksByAccount.get(accountId) : 0;
     Double eventCount = eventsByAccount.containsKey(accountId) ? eventsByAccount.get(accountId) : 0;
     Account acc = new Account(Id = accountId, Open_Activities__c=(taskCount + eventCount) );
     System.debug('coming here');
     acctToUpdate.add(acc);    
   }    

   update acctToUpdate;

  }
}
Trigger:
trigger OpenActivityCountTrigger on Task (before insert,after update) {
Set<Id> acctIds = new Set<Id>();
String accPrefix = Account.SObjectType.getDescribe().getKeyPrefix();
  for (Task t : trigger.New) {
    //I Have to check this is an account and not something else
    if (string.valueof(t.WhatId).startsWith(Account.SObjectType.getDescribe().getKeyPrefix())) {
      acctIds.add(t.WhatId);
        System.debug('What is going on??');
    }
  }
    
  openActivityCounter.activityCount(acctIds);
    System.debug('Are you even executing after this?');
}
 
Somya TiwariSomya Tiwari
Hi Nabeel,

I think you are getting confused with List and Record. Here have a look at your code.
Id accId = (Id) agg.get('WhatId');
agg in your code is a record, not a MAP. Hence it must work as follow:
Id accId = (Id) agg.WhatId;
Do mark the question as solved, and give a like if my answer helped you in any way.

Regards,
Somya Tiwari