Apex - Enforcing Sharing Rules
First thing first- How can we enforce sharing rules in Apex class? Apex generally runs in system context; that is, the current user's permissions and field-level security aren’t taken into account during code execution. Sharing rules, however, are not always bypassed: the class must be declared with the without sharing keyword in order to ensure that sharing rules are not enforced. Use with sharing keyword to enforce sharing rules of the current user(Doesn't includes the user's permissions and field-level security). Some important notes✍- 1) Enforcing sharing rules by using the with sharing keyword doesn’t enforce the user's permissions and field-level security. This again, pertains to only respecting OWDs and Sharing Rules. 2) If a class is declared as "with sharing" then, the sharing settings apply to all code contained in the class, including initialization code, constructors and methods. However, Inner Classes DO NOT inherit sharing settings from the co...