You need to sign in to do that
Don't have an account?
Mohit Kumawat 20
Sharing Keyword Use in Container class
Consider the case:
public with sharing class containerClass { ... public class innerClass { ... } }I know that the innerClass does not inherit the sharing setting of the containerClass and [without sharing] is the default for the inner class. Then what is the use of specifying with sharing in the container class.
Rules aren't enforced, developers who use Apex must take care that they don't inadvertently expose sensitive data that would normally be hidden from users by user permissions, field-level security, or organization-wide defaults. They should be particularly careful with Web services, which can be restricted by permissions, but execute in system context once they are initiated.
This example has two classes, the first class (CWith) enforces sharing rules while the second class (CWithout) doesn’t. The CWithout class calls a method from the first, which runs with sharing rules enforced. The CWithout class contains an inner classes, in which code executes under the same sharing context as the caller. It also contains a class that extends it, which inherits its without sharing setting.
Enforcing the current user's sharing rules can impact:
-SOQL and SOSL queries. A query may return fewer rows than it would be operating in the system context.
-DML operations. An operation may fail because the current user doesn't have the correct permissions. For example, if the user specifies a foreign key value that exists in the organization, but which the current user does not have access to.
Shubham Nandwana.
AppPerfect Corp.
salesforce@appperfect.com
408-252-4100
http://www.appperfect.com/services/salesforce/
Salesforce Development & Operations Experts
All Answers
I have written a few example case scenarios, as below :
Say I have 3 classes:
public with sharing class A {}
public without sharing class B{}
public class C{} // Class C is a non-specified-sharing class.
Now, let's consider the following case scenarios:
class B extends A // class B's code now executes in class A's mode, i.e. in "with sharing" mode.
class B calls class A // called code will now be executed in the mode of the class in which it was defined, in this case, in class A's mode, i.e. in "with sharing" mode.
class C calls class A // called method's code in class A will execute in class A's mode, i.e. in "with sharing" mode.
class C extends class A // code in class C now executes in the parent class A's mode, i.e. in "with sharing" mode.
class A calls C // code in class C is executed in class C's mode, i.e. in "without sharing" mode although it's sharing settings are unspecified in the class' declaration.
Hope this solution helps.
Thank You
Ajay Dubedi
Rules aren't enforced, developers who use Apex must take care that they don't inadvertently expose sensitive data that would normally be hidden from users by user permissions, field-level security, or organization-wide defaults. They should be particularly careful with Web services, which can be restricted by permissions, but execute in system context once they are initiated.
This example has two classes, the first class (CWith) enforces sharing rules while the second class (CWithout) doesn’t. The CWithout class calls a method from the first, which runs with sharing rules enforced. The CWithout class contains an inner classes, in which code executes under the same sharing context as the caller. It also contains a class that extends it, which inherits its without sharing setting.
Enforcing the current user's sharing rules can impact:
-SOQL and SOSL queries. A query may return fewer rows than it would be operating in the system context.
-DML operations. An operation may fail because the current user doesn't have the correct permissions. For example, if the user specifies a foreign key value that exists in the organization, but which the current user does not have access to.
Shubham Nandwana.
AppPerfect Corp.
salesforce@appperfect.com
408-252-4100
http://www.appperfect.com/services/salesforce/
Salesforce Development & Operations Experts