+ Start a Discussion
Mohit Kumawat 20Mohit 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.                                        
Best Answer chosen by Mohit Kumawat 20
Shubham NandwanaShubham Nandwana
Hi Mohit,
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.
public with sharing class CWith {
  // All code in this class operates with enforced sharing rules.

  Account a = [SELECT . . . ];

  public static void m() { . . . }
  
  static {
    . . .
  }

  {
    . . .
  }

  public void c() {
    . . .
  } 
}

public without sharing class CWithout {
  // All code in this class ignores sharing rules and operates 
  // as if the context user has the Modify All Data permission.
  Account a = [SELECT . . . ];
  . . .

  public static void m() {  
     . . . 

    // This call into CWith operates with enforced sharing rules
    // for the context user. When the call finishes, the code execution 
    // returns to without sharing mode.
    CWith.m();
  }


  public class CInner {
    // All code in this class executes with the same sharing context
    // as the code that calls it. 
    // Inner classes are separate from outer classes.
    . . .

    // Again, this call into CWith operates with enforced sharing rules
    // for the context user, regardless of the class that initially called this inner class.
    // When the call finishes, the code execution returns to the sharing mode that was used to call this inner class.
    CWith.m();
  }

  public class CInnerWithOut extends CWithout {
    // All code in this class ignores sharing rules because
    // this class extends a parent class that ignores sharing rules.
  }
}

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

Ajay K DubediAjay K Dubedi
Hi Mohit,

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
Shubham NandwanaShubham Nandwana
Hi Mohit,
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.
public with sharing class CWith {
  // All code in this class operates with enforced sharing rules.

  Account a = [SELECT . . . ];

  public static void m() { . . . }
  
  static {
    . . .
  }

  {
    . . .
  }

  public void c() {
    . . .
  } 
}

public without sharing class CWithout {
  // All code in this class ignores sharing rules and operates 
  // as if the context user has the Modify All Data permission.
  Account a = [SELECT . . . ];
  . . .

  public static void m() {  
     . . . 

    // This call into CWith operates with enforced sharing rules
    // for the context user. When the call finishes, the code execution 
    // returns to without sharing mode.
    CWith.m();
  }


  public class CInner {
    // All code in this class executes with the same sharing context
    // as the code that calls it. 
    // Inner classes are separate from outer classes.
    . . .

    // Again, this call into CWith operates with enforced sharing rules
    // for the context user, regardless of the class that initially called this inner class.
    // When the call finishes, the code execution returns to the sharing mode that was used to call this inner class.
    CWith.m();
  }

  public class CInnerWithOut extends CWithout {
    // All code in this class ignores sharing rules because
    // this class extends a parent class that ignores sharing rules.
  }
}

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
This was selected as the best answer