Counting Open and Closed Activities in Salesforce
The problem: I needed to log how many open Activities there were on Leads, Contacts, Opportunities, and Accounts as well as Activity History.
I have similar triggers in the past but in this case I wanted a more flexible approach that would work across multiple objects.
Using standard reporting you can report on Objects with or without Activity records. You could also explore solutions such as Rollup Helper but in this case the customer wanted to see an easy count total on the top level of the object.
Before I wrote any code I had a look around the wonder-web and found a great bit of code that took me 60% of the way there.
A huge thank you goes out to rjpalombo for his initial work on Counting Open Activities. The blog below takes his example code and adds counting of closed Activities.
The following code example just expands on rjpalombo's code so that I could count Activity History as well as Open Activities.