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
Amy ThroppAmy Thropp 

How to query activityhistory

I have a custom object that has related activities. I want to query events, current or in history, related to that custom object.

I can't seem to get around the error message that the activityhistory does support query.

Any suggestions on how to get around this? I know you can use activity history in a subquery, I just can't seem to get the syntax right.
Terence_ChiuTerence_Chiu
Amy, have your tried querying the Event or Task object instead ? For instance:

SELECT Subject FROM Event WHERE WhatId = '<Id of custom object record>'
Amy ThroppAmy Thropp
the activities will be mostly in the past so I don't think I can see them in the events table.
Terence_ChiuTerence_Chiu
Have you tried the following query ?

select Name, (select activitydate, subject from activityhistories) from <Custom_Object__c> where id=
'<Id of custom object record>'
 
Amy ThroppAmy Thropp
Works in the SF Developer’s Console, but not through the API – Doesn’t recognize activityhistories as a valid table. Original question: I have a custom object that has related activities. I want to query events, current or in history, related to that custom object. I can't seem to get around the error message that the activityhistory does support query. Any suggestions on how to get around this? I know you can use activity history in a subquery, I just can't seem to get the syntax right. Reply: Have you tried the following query ? select Name, (select activitydate, subject from activityhistories) from where id= ''
Michael Glassmaker75Michael Glassmaker75

Amy   try this nested query approach:

  (you can alter nested SOQL to pull fields from your Activity Obj)  as well can alter outer SOQL 

*This pulls from Case as Parent

SELECT Id, (SELECT Id, Subject, CreatedDate FROM ActivityHistories WHERE CreatedDate = LAST_N_DAYS: 90) FROM Case WHERE Id != null AND Recordtype.Name='XYZ' order by lastmodifieddate DESC