+ Start a Discussion

Dynamic SOQL not returning fields hidden by Field Level Security

I was under the impression that a describe call should access all fields on an object, regardless of whether or not they are hidden by FLS, am I wrong here?  A dynamic SOQL statement that I have is not returning hidden fields:


Code snippet:


//describe call to get lead fields for dynamic SOQL Map<String, Schema.SObjectField> leadFieldMap = Schema.SObjectType.Lead.fields.getMap(); //variable to hold dynamic SOQL of lead fields string querySOQL; // get all open leads public integer loadAllLeads() { system.debug ('lead schema map: ' + leadFieldMap); querySOQL = 'SELECT '; querySOQL += '(select campaignid from campaignmembers order by createddate desc),'; for (string fieldname : leadFieldMap.keyset()) { querySOQL += fieldname + ', '; } querySOQL = querySOQL.substring(0,querySOQL.length()-2); //lop off trailing comma querySOQL += ' FROM lead WHERE isConverted = false'; querySOQL += ' ORDER BY createddate limit :queryLimit'; system.debug ('query string: ' + querySOQL); leads = Database.Query(querySOQL); if (leads.isEmpty()) { return 0; } else { matchLeads(); return leads.size(); } }





Whether it is a dynamic SOQL or Describe call, first it will check the FLS;

If the FLS is hidden then it will not fetch that field.


My understanding is that Apex runs in system mode unless you use System.runAs(User). I would expect Describes and dynamic SOQL to work that way, too. So you should have access to all fields in your Apex.