1

I am creating a dynamic query by passing the object using Schema.getGlobalDescribe(). The result includes system fields that you cannot query (in workbench or query editor). Example: For contact object, the total fields retrieved includes hasoptedoutofemail, hasoptedoutoffax, donotcall fields which you cannot query further to retrieve data.

Any suggestions please?

2 Answers 2

0

I check this in my org and I don't get the fields hasoptedoutoffax, donotcall on contact using describe.

Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get('Contact').getDescribe().fields.getMap();

for(String str : objectFields.keySet())
    system.debug(str);

I only found the hasoptedoutofemail in describe. When I query on this I get the result.

enter image description here

If you still want to filter fields, you can use DescribeFieldResult Class to filter out field based on if current user can create/access the field. If field is custom or standard.

0

What you are facing I get typically when the schema call is in a utility apex class constructing a SOQL string and the final database.query() resides in a different class, such as a controller or trigger. Now if both apex files got different API Versions, the Schema call returns potentially obsolete fields which might not exists in the other class.

I make heavy use of schema since I write almost ALL queries with the help of utility functions to allow SELECT * FROM ... because I hate long and hard to maintain lists of field names as we should write it in vanilla apex.

If this might be true in your case, just keep all your apex classes at the same API version and you will be fine. If you can't match the versions, you could selectively exclude those fields. I did that with an self made query syntax looking like this SELECT *.not(fieldname1|fieldname2|....)

More details and an older version of my utility class you can find here

Howto wildcard SELECT * to query all fields of objects in APEX?

You must log in to answer this question.

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