7

We're using Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap()to fetch all the fields in Product2 and build a dynamic query. When the string is used in database.query() method, it throws the error below.

No such column 'IsArchived' on entity 'Product2'. 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.

The query works when I remove "IsArchived" field. Tried running the code in Execute Anonymous and I'm not getting the same issue. Query retrieves records even when 'IsArchived' is added.

4

It sounds like you've got a mix of API versions in your classes and/or triggers, and this is causing a problem. If you use a utility class to generate your dynamic queries, please make sure the API version is no higher than all other classes that use this utility class. This can cause new fields/objects to be exposed to older versions and cause runtime exceptions such as this one. In this case, it appears your utility class is either API version 43.0 or 44.0, and the class that's trying to use the dynamic query is lower than version 43.0. For an example of a prior version of this error, see this Known Issue. Also read this answer I wrote about why you should keep your API versions all the same.


As for a solution, consider reducing your utility class(es) down to version 27.0 or so (this is the first time I'm aware of where API versions break describe calls), or bump up the classes that use this/these utility classes to at least version 43.0.

5

You should check if the field is accessible before adding it to your query.

for (SObjectField field : SObjectType.Product2.fields.getMap().values())
{
    if (field.getDescribe().isAccessible())
    {
        // now you can include it in your query
    }
}
  • Thanks! Will try this. I'm using the same user to run the Batch Class and the Query in Dev Console. Not sure why I'm not getting the issue in the latter. – jema Dec 10 '18 at 17:17
  • 1
    It runs in a different user context. – Adrian Larson Dec 10 '18 at 17:18
  • 2
    This is (most likely) an API version issue, not a field permissions issue. – sfdcfox Dec 10 '18 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.