I am checking for field accessibility of an User u whose Profile is p in apex Test class.To this profile field level security is set to Hidden for the field team_name__c.When I run as this user using runAs() method, and check the field accessibility as below, it returns false.

system.debug('Accessible---> '+Schema.SObjectType.MyCustomObject__c.fields.team_Name__c.isAccessible());
//prints false

But according to the doc

The system method runAs enables you to write test methods that change the user context to an existing user or a new user so that the user’s record sharing is enforced. The runAs method doesn’t enforce user permissions or field-level permissions, only record sharing.

Which means field should have been accessible to the User within runAs() method as it doesn't enforce field level permissions.Please correct me if my understanding is wrong.

  • Can you show us more code? Are you double sure the user have right profile? Also did you checked if this profile/user have anything that could oweride this access level? Commented Mar 28, 2014 at 8:04
  • @ArturKępczyński As I mentioned in my question the profile assigned to user has visibility Hidden to the field.
    – codebandit
    Commented Mar 28, 2014 at 8:26
  • @ArturKępczyński:regarding more code,I am just setting up User and Profile objects and nothing else.
    – codebandit
    Commented Mar 28, 2014 at 8:29
  • 1
    Some ancient history about platform bugs in this area developer.salesforce.com/forums?id=906F00000008xv9IAA. Later in the thread FLS problems are mentioned, and it sounds like the intention is that System.runAs should allow FLS to be tested.
    – Keith C
    Commented Mar 28, 2014 at 9:23
  • 1
    That is how I read it, but those (the field level ones) are not comments from salesforce people so it is not definitive. And it sounds like the object-level bug (caching across test methods) may have been present in the field level case too and that may or may not have been fixed. Hopefully someone will answer your question with solid information about what they see today in the platform.
    – Keith C
    Commented Mar 28, 2014 at 9:50

1 Answer 1


Apex will not enforce CRUD and FLS no matter what you do -- but you can test for CRUD and FLS using the method you documented in your code sample.

You must write your own handling code to 'gracefully degrade' your app should the running user not have the appropriate (read, create, update, delete) on the object or field within the object.

I'm sure you've read these docs a dozen times, but I'm posting it here for completeness of the answer:



developers should consider having graceful degradation within their applications if appropriate for their design.

  • Nice answer! Note the exception of ExecAnon, which enforces CRUD and FLS :-) Commented May 28, 2014 at 21:27

You must log in to answer this question.

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