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:

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...