Does anyone know what soql queries you would run on Task and Event equivalent to what salesforce uses to generate the Activity History and Open Activities related lists.

The OpenActivity and ActivityHistory views get you this but they don't work for my use-case as I need to perform aggregate queries and I have a custom formula field which shows as null when queried through those views.

In general I know that it is something like the following:

contact/lead task: select id from task where whoid = :recordID
    history: and isclosed = true
    open: and isclosed = false
account/opp/.../custom object event: select id from event where whatid = :recordID
    history: and ((isalldayevent = true and activitydate <= today) OR (isalldayevent = false and enddatetime < now)

but there is a lot of special logic. e.g.

  • accounts and parent accounts will see activities related to their contacts
  • I think there are some nuances around how ActivityDate is handled for recurring tasks/events as well.
  • manywho (shared activities) and invitee events need some special handling as well I believe.

1 Answer 1


Greg, would queries like the following solve your problem?

SELECT Id, Name,
    (SELECT Id FROM OpenActivities),
    (SELECT Id FROM ActivityHistories)
FROM Account

You can find more information on OpenActivity and ActivityHistory in the SOAP API Developer's Guide.

  • Unfortunately not, as I mentioned in my question I'm familiar with OpenActivity and ActivityHistory but they don't work for my use-case. Commented Nov 6, 2014 at 4:44
  • 2
    Hello, Greg, if you can add additional clarification on what exactly you're trying to do perhaps someone can make a better suggestion. I feel there's got to be additional context or constraints around what you're trying to do that makes it possible to do aggregate querying. Are you doing this in Visualforce? Batch Apex?
    – Marty C.
    Commented Nov 6, 2014 at 15:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .