Basic unit test to check if I have read access to a field – Apex

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!

Answer

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

Attribution
Source : Link , Question Author : Daft , Answer Author : Sam

Leave a Comment