5

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!

2
  • Unit tests don't return error messages to VF pages. Everything is written to a log file.
    – crmprogdev
    Jun 11, 2014 at 15:27
  • Is this for a VF controller or a different context?
    – Mike Chale
    Jun 11, 2014 at 15:34

2 Answers 2

5

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());
2
-1

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.

4
  • 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, 2014 at 15:47
  • So you want us to write your code for you? Here's a link to a good SF search engine: findsf.info. 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, 2014 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, 2014 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, 2014 at 15:56

This site is temporarily in read-only mode and not accepting new answers.

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