Reputation: 1
field for number of Activities for each Contact, regardless of Type or Status. Full Details: The requirement is to be able to create reports based on Contacts or account or Opportunities and see a “Summary” field for Total # of Activities associated with either Contacts, Accounts or Opportunities. Note that we do not want to see a line for each activity, we want to see one line for the each contact or opp or account with the activity summary count. Note: The original request also included the ability to have a unique activity count on reports
Upvotes: 0
Views: 1189
Reputation: 31
I haven't tested this code, but you could do something like this for an insert (you'd need to also cover updates and deletes). In this example, NumberOfActivites__c is your custom Task count field on the Contact object:
Map<Id,Integer> countMap = new Map<Id,Integer>();
List<Contact> contactList = new List<Contact>();
for (Task t : trigger.new){
//get id's of all contacts affected by the batch
Id w = t.whoId;
if (w.getSObjectType().getDescribe().getName() == 'Contact'){
//since there could be more than one task related to a contact
//in a batch, you would have to count them
if (countMap.keyset().containts(w)){
countMap.get(w) += 1;
} else {
countMap.put(w,1);
}
}
}
//get list of contacts to be updated
contactList = [Select Id, NumberOfActivities__c
From Contact
Where Id In :countMap.keyset()];
//modify contacts in list with new count
for (Contact c : contactList){
c.NumberOfActivites__c = c.NumberOfActivites__c + countMap.get(c.Id));
}
//do the update
update contactList;
Upvotes: 0