Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I am trying to load all tasks of a contact via SOQL query:

SELECT Id, Subject FROM Task WHERE WhoId = 'contactId'

This query returns only partial list of Tasks. I.e. in UI I see tasks directly attached to this Contact which are not being returned as part of the result for this query.

I googled a bit and found out that I might need to query an ActivityHistory sobject and yes querying it returns me "missing" tasks as well:

SELECT (SELECT Id, Subject FROM ActivityHistories) FROM Contact WHERE Id ='contactId'

So, my question is that an expected behavior? I.e. there is no way to load all the Tasks using 1 query and I have to load both Tasks and AcitivityHistories?

Also, interestingly, I couldn't reproduce this situation in a developer account: I've registered a Task with subject 'other' that took place in the past and in UI it is displayed in Activity History section, but I receive it is a part of normal Task loading query. So, i am not really sure why the Task in originally described situation is not returned with simple Task query (i.e. only in ActivityHistories query)

share|improve this question

1 Answer 1

up vote 9 down vote accepted

Salesforce archives older activities. From the docs:

How can I view activities that are more than a year old?
Salesforce archives older activities according to the conditions below. These activities can be viewed only in the Printable View of the associated record, the Activity History related list of the associated record, export files, or by clicking View All on the Activity History related list. You can search for archived activities using advanced search. Administrators can also delete archived activities using Mass Delete.

  1. Events with a due date greater than 365 days old
  2. Closed tasks with a due date greater than 365 days old
  3. Closed tasks without a due date that were created more than 365 days ago

When these conditions are meet* the Task no longer appears in SOQL queries against the Task sObject. Instead you need to use the ActivityHistory "view" in a relationship query from the parent sObject.

* It appears the archival process isn't immediate. If you set a Tasks Due Date to be more than one year old it will still appear against the Task sObject for a period of time. Most likely Salesforce has a scheduled job to process the archival.


Useful notes from the docs:

  • ActivityHistory a "read-only object contains information about each activity related to an object". "This object helps you replicate the related list functionality of the user interface."
    So ActivityHistory is more like a view that collectively returns both Tasks and Events and archived records.
  • Task "Represents a business activity such as making a phone call or other “to-do” items. In the user interface, task and Event records are collectively referred to as activities."
  • Event "Represents a calendar appointment event. In the user interface, Event and Task records are collectively referred to as activities."

A couple of other thoughts as to why you might see a record in ActivityHistory but not in Task.

  1. The record in question is an Event and not a Task. Check the keyprefix in the Id. If it is 00U then you are dealing with an Event. A Task would be 00T. Or check the IsTask ActivityHistory column.
    As per the discussion, we have confirmed that you are dealing with a task
  2. There might be sharing rules/permission issues hiding the record in question. Does the user running the query have the View All Data permission?
  3. The task might have a due date greater than 365 days old and has been archived by Salesforce. See How can I view activities that are more than a year old?
share|improve this answer
    
thanks for the response, though I don't understand what you're trying to say. I know that one cannot query the ActivityHistory directly (take a look at my example query - it works, and it doesn't query directly). And like you mentioned tasks + events are activities, hence tasks end up in Activity History section of the UI when they are done (or took place in past). My question is why might Task only be seen via ActivityHistory sobject? (and how to replicate that) –  antonlitvinenko Feb 18 '13 at 19:33
    
Sorry, I'm getting there in a very round about way. I suspect that the record in question actually an Event rather than a Task that you are seeing via the ActivityHistory. Check the keyprefix on the problem record. Is it 00U or 00T? –  Daniel Ballinger Feb 18 '13 at 19:39
    
It is 00T and it has IsTask=true. So, it is actually a Task, right? –  antonlitvinenko Feb 18 '13 at 19:46
1  
It might be that the Task in question has been archived. How old is the due date on the problem record? Is it older than a year? –  Daniel Ballinger Feb 18 '13 at 20:00
1  
@antonlitvinenko Yes, that certainly seems to be the case. As ActivityHistory is setup to replicate the UI behaviour you can see archived tasks for a single lead/contact. –  Daniel Ballinger Feb 18 '13 at 20:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.