Wednesday 12 October 2016

How to check field permission(FLS) based on profile via Apex or SOQL in Salesforce

Let's take an example, you are developing a visualforce page with custom controller, And you have to display the field as either input or output mode based on the FLS permission for the specific field at the profile level who is running the page.


  1. Boolean hasEditInvoiceField = false;

  2. List<FieldPermissions> invoiceOrderNumberFLS = [SELECT SobjectType, Field, PermissionsRead, PermissionsEdit, Parent.ProfileId FROM FieldPermissions where SobjectType = 'Opportunity' and Field='Opportunity.Invoice_OrderNumber__c' AND Parent.ProfileId=:Userinfo.getProfileId()];

  3. if(!invoiceOrderNumberFLS.isEmpty()){
  4.     hasEditInvoiceField = invoiceOrderNumberFLS[0].PermissionsEdit;
  5.     System.debug('####'+hasEditInvoiceField);
  6. }


Note: 
1. Invoice_OrderNumber__c is the custom field on Opportunity Object. 
2. If the profile does not have permission(Visible, ReadOnly), then query will return zero results.
3. PermissionsEdit field on FieldPermission object will return the Boolean value as below,
  • If field have Visible access alone for the specified field then it return TRUE.
  • If field have Visible and Read only access for the specified field then it return FALSE.

Reference:

9 comments:

  1. Nice blog and too informative. The points presents in the blog is so impressive. Update more blog like this. Really appreciating for doing the blog like this. Thanks for sharing. Keep on updating more information. Eagarly waiting for you updates.tech service.
    SEO Web Design Company Chennai

    ReplyDelete
  2. very informative blog and useful article thank you for sharing with us , keep posting learn more aboutsalesforce training,salesforce online training

    ReplyDelete
  3. this is really too useful and have more ideas from yours. keep sharing many techniques. eagerly waiting for your new blog and useful information. keep doing more.
    salesforce training in chennai

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
    Salesforce Training in Chennai
    German Classes in Chennai
    Salesforce Course
    Salesforce Developer Training
    German Language Course in Chennai
    German Courses in Chennai

    ReplyDelete
  6. With Bulk Object Field Creator (BOFC) App user can (Compare or Export or Update) field permissions for multiple objects and for multiple profiles within same Salesforce org in few clicks.

    ReplyDelete

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...