There is a field named "Email Opt Out" in the contact object. Can we query this field and put in the filter condition if the "Email Opt Out" field does not have both "Field-Level Security" and "Field Accessibility" in the apex class.
Apex doesn't enforce FLS:
Although Apex doesn't enforce object-level and field-level permissions by default, you can enforce these permissions in your code by explicitly calling the sObject describe result methods (of Schema.DescribeSObjectResult) and the field describe result methods (of Schema.DescribeFieldResult) that check the current user's access permission levels. In this way, you can verify if the current user has the necessary permissions, and only if he or she has sufficient permissions, you can then perform a specific DML operation or a query.
- You can use user-inaccessible fields in a SOQL where clause
- You can select user-inaccessible fields in a SOQL query, and so if you serialize an sObject in Apex you will include all selected fields, whether accessible to the user or not
You can perfectly query a field in a SOQL query that isn't visible to the user, you can filter on it, but you won't see it in visualforce or in the layout.
Unless you make the field visible ofcourse.