The Use of “With Sharing” and “Without Sharing” is to enforce the sharing rules.
With sharing keyword:
It allows you to specify the sharing rules for the current
user be taken into the account for Apex class.
Because apex classes will always runs in system context.
In System context Apex code has access
to all objects and fields— object permissions, field-level security, sharing
rules aren’t applied for the current user.
This is to ensure that code won’t fail to run because of
hidden fields or objects for a user. If a class is defined as with sharing, the
method within that class will act as with sharing.
Public Class with sharing apexsharingclass
{
// sample code
}
Without Sharing:
Without sharing will ensure that the class will not enforce
the sharing rules for the current user.
Public class without sharing apexclass
{
// your code.
}
Note:
If the method within “with sharing” class is called by a “without
sharing” class, then with sharing method will execute with sharing rule
enforced.
Inner classes do not inherit the sharing setting from their
container class.
Classes inherit the sharing setting from a parent class when
one class extends or implements another.
No comments:
Post a Comment