function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Amil AbdallahAmil Abdallah 

Can't display results for whoid and whatid from Activity History

I have created a salesforce page which displays a datatable that is populated with an Account's activity history.  I have created this table view as opposed to utilizing the related list view because the activity history gets very convoluted with email responses.  The clients have requested a table view in which to filter out emails and other types of activities so that it's much more managable/easier to read. 

In doing my research, I have come to find that the Activity History table is special and cannot be queried directly.  Inconvenient as it is, I went ahead and created a soql query which joins the activity history to the account object as seen below:

[select id, name, ownerid,
            (select id, subject, whoid, whatid, accountid, ownerid, activitydate, description, status, activitytype, istask, CreatedById from ActivityHistories WHERE not(subject like 'mass email%' OR subject like 'pardot list email%') ORDER BY activitydate DESC)
            from account
            where id = :ApexPages.currentPage().getParameters().get('id')]

The query works and I am able to display the data within the table with the exception of two fields, whoid ('name/contact' field) and whatid ('related to' field). When I run this query in the Force.com explorer I clearly see value for those fields populated in the results, however, when I query within the developer console, those fields return null. I am unable to display these fields in my table as they show up null in the result set. Does anyone know why I am unable to get these results within my apex code/visualforce page despite the fact that I can see the results in the force.com explorer? Any help is greatly appreciated.
Best Answer chosen by Amil Abdallah
Amil AbdallahAmil Abdallah
The issue turned out to be the API version.  For API version 30.0 or later, the returned value of the WhoId field matches the value in the queried object, not necessarily in the activity record itself.  I needed the whoid to point to the lead/contact id of the specific activity which seems to have been removed in versions 30+.  In order to achieve the functionality I needed, I had to revert my class and page back to API version 29.0 and all is working now!

All Answers

shiv@SFDCshiv@SFDC
Hi Amil by seeing your code. it looks like that you are trying to show activity related to particular account  on custom visualforce page.

When you are checking the priview are you passing Id parmeter in URL ?

Like : https://Your_instance_Name/apex/YourVF_Page_Name?id='recordId'
Amil AbdallahAmil Abdallah
Yes.  I have added a button when viewing an account page.  The button opens a new window to display the activity table.  The account id is passed to the page in the URL as you have described.  I can query and retrieve the data fine.  I just want to know why I am able to get the whoid and whatid from the Activity History table through the Force.com Explorer, but not through Visualforce/developer console.   
shiv@SFDCshiv@SFDC
Through developer console how you are setting the current page and and id in url ?

what it will return ?
ApexPages.currentPage().getParameters().get('id')
Amil AbdallahAmil Abdallah
When I query the tables directly I manually replace  "ApexPages.currentPage().getParameters().get('id')" with an account id from within our system.
Amil AbdallahAmil Abdallah
The issue turned out to be the API version.  For API version 30.0 or later, the returned value of the WhoId field matches the value in the queried object, not necessarily in the activity record itself.  I needed the whoid to point to the lead/contact id of the specific activity which seems to have been removed in versions 30+.  In order to achieve the functionality I needed, I had to revert my class and page back to API version 29.0 and all is working now!
This was selected as the best answer
Matt WhalleyMatt Whalley
The chosen correct answer is incorrect. Yes, the API had something to do with it, but that is not whole issue.  In API 30, shared activities were introduced.  So WhoId may not return anything, but PrimaryWhoId will.  Same goes for Who.Name > PrimaryWho.Name.  So you could have put in PrimaryWhoId and use the newer API stuff.

Read all about it here: https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_activityhistory.htm