I am trying to fetch the fields and their types of few Sobjects dynamically and trying to use these to query and do some operations.

Code below for reference :

Schema.DescribeSObjectResult d = Schema.Sobjecttype.CustomObject__c;
Map<String,String> fMap = new Map<String,String>();
Map<String, Schema.SObjectField> M = d.fields.getMap();
List <Schema.SObjectField> fieldList = M.values();

for(Schema.SObjectField aField: fieldList){
    Schema.DescribefieldResult fieldDes = aField.getDescribe();
return fMap;

The map returned here contains a field which is not available in the object description. May I know what is causing this issue and where the fields are coming from.

Exception that I get while building the query with the above returned fields is System.QueryException: No such column 'LastReferencedDate' on entity 'CustomObject__c'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.

Thanks in advance for any help!

  • Is the with sharing on for the class? And user having View All Data permission?
    – SEuser
    Aug 14, 2014 at 7:06
  • Sharing is ON. User is a System Admin. They've all the access to the Custom object and its fields.
    – Ashu NCS
    Aug 14, 2014 at 7:13

1 Answer 1


The problem is probably related to this (nominally fixed) known issue. We have seen the describe call return all the fields available in the latest API version when called from a class marked with an earlier API version.

Move your classes to the latest API version so what the describe call returns and what is acceptable in the query are the same. Or add a hard coded set of field names to ignore and filter those out of the describe results.

  • Thanks for that info but I see that my class from when I am describing and retrieving the map is of version 29 and I still get this issue :(
    – Ashu NCS
    Aug 14, 2014 at 7:23
  • @Ashu Have you tried moving to the latest API version (31) as I suggested? The field LastReferencedDate was introduced in that version and I think the bug isn't really fixed.
    – Keith C
    Aug 14, 2014 at 8:23
  • Hi Keith, I have updated the version to 31 but still face the same issue.
    – Ashu NCS
    Aug 14, 2014 at 9:55
  • @Ashu I'm surprised. If the class that does the describe work and the class that does the SOQL are both at API 31 I would expect there to be no problem. But if you can't get that to work the workaround is "Or add a hard coded set of field names to ignore and filter those out of the describe results".
    – Keith C
    Aug 14, 2014 at 10:00
  • Keith, I've used workaround to get rid of those newly added fields. Thanks for your help!
    – Ashu NCS
    Aug 18, 2014 at 5:19

You must log in to answer this question.

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