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

History table query problem

CustomObject__History[] histories = [Select Id, CreatedDate, CreatedBy.user_id__c, Field, OldValue, NewValue FROM CustomObject__c 


In Apex query, the system does not recognize the CUSTOM field on user called user_id and gives an error: No such column 'user_id__c' on entity 'Name'.


If I change it to SOME standard field like CreatedBy.Name, CreatedBy.Email or CreatedBy.UserName it works. It does NOT work for some standard fields such as Department, CommunityNickName etc.  At first it appeared that it doesn't work for Nillable fields, but it works for FirstName and Title which is Nillable!


I am logged in as system admin and have all fields visible.









check the field level security for those fields.


Logged in as sys admin and have all fields visible.


Sean TanSean Tan

I'm pretty sure this is related to the fact that the CreatedBy field on the history tables is Polymorphic. It's interesting because on standard  / custom objects you can do what you're trying to do without a problem.


Here are a couple of things you can try:


  1. Use the TYPEOF in the SOQL query (this may or may not work, I haven't tested it out myself and TYPEOF is not supported on every type of org) See here for more info on TYPEOF:
  2. Query the histories with the CreatedById field and re-query the User object after the fact to get any additional information you need.



Thanks. I will try it out. It would appear to be that createdById is NOT polymorphic. The OwnerId can be (a user or a queue).


#2 is an option, but I am using this as part of Data Loader integeration where I am exporting transactions for a given object using History tables. I would really have to have another table on my SQL server to get the user id and do a join. I might just hack it with Title field it if does not work.