PDI · Question #210
In the following example, which sharing context will myMethod execute when it is invoked?
The correct answer is B. Sharing rules will be Inherited from the calling context. When an Apex method's sharing context is not explicitly defined, it inherits the sharing context of the class or method that invoked it.
Question
In the following example, which sharing context will myMethod execute when it is invoked?
Options
- ASharing rules will not be enforced for the running user.
- BSharing rules will be Inherited from the calling context.
- CSharing rules will be enforced by the instantiating class.
- DSharing rules will be enforced for the running user.
How the community answered
(70 responses)- A7% (5)
- B87% (61)
- C4% (3)
- D1% (1)
Why each option
When an Apex method's sharing context is not explicitly defined, it inherits the sharing context of the class or method that invoked it.
This describes `without sharing` behavior, which is only explicit or inherited from a `without sharing` context.
By default, if an Apex class or method is not explicitly declared with `with sharing` or `without sharing`, its sharing rules are inherited from the calling context. This means if it's called by a `with sharing` class, it runs `with sharing`; if called by a `without sharing` class, it runs `without sharing`. If called from outside Apex (e.g., a trigger or a Visualforce page that is running `with sharing`), it runs `with sharing`.
Sharing rules are determined by the class definition (`with sharing`, `without sharing`) or inheritance, not solely by the "instantiating class" which isn't a standard concept for sharing context.
This describes `with sharing` behavior, which is only explicit or inherited from a `with sharing` context. The default is not always enforced.
Concept tested: Apex class sharing context inheritance
Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_sharing.htm
Topics
Community Discussion
No community discussion yet for this question.