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.
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,
- Boolean hasEditInvoiceField = false;
- 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()];
- if(!invoiceOrderNumberFLS.isEmpty()){
- hasEditInvoiceField = invoiceOrderNumberFLS[0].PermissionsEdit;
- System.debug('####'+hasEditInvoiceField);
- }
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: