1

I have a doubt regarding sharing mechanism in Salesforce. If we have these classes:

public without sharing class Ctrl1 {

}


public with sharing class Ctrl2 extends Ctrl1 {

}

By reading the official docs, it's not clear if Ctrl2 class will be with sharing or without sharing. It says it inherits, but it does not specify the behaviour if you override.

What about the opposite? If Ctrl1 is with sharing and Ctrl2 id without sharing? Thanks

1 Answer 1

3

From the documentation:

  • The sharing setting of the class where the method is defined is applied, not of the class where the method is called. For example, if a method is defined in a class declared with with sharing is called by a class declared with without sharing, the method will execute with sharing rules enforced.
  • If a class isn’t declared as either with or without sharing, the current sharing rules remain in effect. This means that the class doesn’t enforce sharing rules except if it acquires sharing rules from another class. For example, if the class is called by another class that has sharing enforced, then sharing is enforced for the called class.
  • 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.
  • Inner classes do not inherit the sharing setting from their container class.
  • Classes inherit this setting from a parent class when one class extends or implements another.

Sharing settings are inherited by default (specifying a sharing setting overrides this), but sharing settings are also applied as defined in source, if there is one.

So, if you have a child who is with sharing, and a parent who is without sharing, when you call a parent method from a child object, the current sharing context would be without sharing, since the parents sharing setting is set that way.

Theres additional information in the documentation for sharing keywords.

3
  • yes. I linked there in the question. It applies for sure when the inheriting class does not anything about sharing. But what about when it says, like in the example I gave? public class Ctrl2 extends Ctrl1 {} ---> I understand it inherits public class with sharing Ctrl2 extends Ctrl1 {} ---> I don't know what happens public class without sharing Ctrl2 extends Ctrl1 {} ---> I don't know what happens Commented Jan 9, 2018 at 14:32
  • 1
    If you've defined a sharing setting in a class, when you run a method from that class, it uses that setting instead of the current sharing setting. Essentially switching between contexts during inheritance. I can see why the default setting would be to carry that setting over to children. I would really recommend setting up some tests in a sandbox org and seeing if it behaves as expected. Commented Jan 9, 2018 at 14:36
  • 2
    Also, I outlined this behavior in this question, which goes pretty in-depth.
    – sfdcfox
    Commented Jan 9, 2018 at 15:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .