Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm currently working on a generic way of implementing FLS on all SOQL calls in my code base.

I want to start by creating a few basic unit tests in Apex to check whether or not I have read access to certain fields.

Could someone point me in the direction of some basic unit tests that could check whether or not I have access to a field?

Any suggestions or advice would be greatly appreciated!

share|improve this question
Unit tests don't return error messages to VF pages. Everything is written to a log file. – crmprogdev Jun 11 '14 at 15:27
I have edited my question. Thanks @crmprogdev – Daft Jun 11 '14 at 15:29
Is this for a VF controller or a different context? – Mike Chale Jun 11 '14 at 15:34
up vote 5 down vote accepted

From your question it looks like you want to check if a field is editable / createable by the current user. I think you can do this by using the DescribeFieldResult class. In the below example I have hardcoded the field which you can easily parameterize.

 Schema.DescribeFieldResult F = Account.CustomerPriority__c.getDescribe();
 system.debug('Is the field Create-able '+ F.isCreateable()); 
 system.debug('Is the field Editable '+ f.isUpdateable());
share|improve this answer
This is what I immediately thought of when I saw this question. Once you have done the describe on the field, there is the "isAccessible" method which might respond with what you want?… – Simon Lawrence Jun 11 '14 at 15:55
Ah I see, much appreciated guys! – Daft Jun 11 '14 at 16:02

I suggest you start by working through the tutorials in the VisualForce Developer's Guide. There's a section covering controllers and controller extensions that includes test classes. You'll also want to look at the last section in particular of the Apex Workbook which covers VF controllers and test classes for them.

share|improve this answer
Thanks @crmprogdev, I appreciate the response. But I need to get somthing basic put together very quickly so I don't really have time to go through the docs. – Daft Jun 11 '14 at 15:47
So you want us to write your code for you? Here's a link to a good SF search engine: Use the right combination of key words and you might find some code snippets in someone's blog or a link that will take you to code examples that are in the SF docs. – crmprogdev Jun 11 '14 at 15:51
Absolutely not, even though it very much sounds that way. I was just hoping for a small example I could build upon. Thanks all the same crmprogdev. – Daft Jun 11 '14 at 15:53
My point being you could at least check the references provided to see if what you need is there rather than dismissing them entirely; especially since this is your first test class. Until you have a test user context, none of anyone else's code on how to detect FLS will be of use to you. – crmprogdev Jun 11 '14 at 15:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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