Play Games

Search This Blog

Friday, December 25, 2015

Difference between with sharing and without sharing keywords

With Sharing:The "With Sharing" keyword allows us to specify that the sharing rules for the current user are enforced(taken into consideration).
Without Sharing(system context) :In general Apex code runs in system context.In system context,Apex code has access to all objects and fields-object permissions,field-level security.Sharing rules are not applied for the current user.
Note:Apex code which is executed through executeAnonymous call has full permissions for the current user.
Important Points: 
1) The sharing setting of the class where the method is defined is applied,not of the class where the    method is called.
Example:If a method is defined in a class declared with with sharing keyword is called by a class declared with withoutsharing keyword,then that method will execute with sharing rules enforced.
2) If a class is not declared as either with or without sharing,the current sharing rules remain in effect.
3) Both inner classes and outer classes can be declared as with sharing.The sharing setting applies to all code contained in the class,including initialization code,constructors and methods
4) Inner classes do not inherit the sharing setting from their container class.

No comments:

Post a Comment