[SalesForce] System.QueryException: No such column ‘fieldName’ on entity ‘CustomObject’

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();
    fMap.put(fieldDes.getName(),String.valueOf(fieldDes.getType()));
}
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!

Best Answer

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.

Related Topic