As an author of the code, I can, of course, know if the context is with or without sharing when I write my code into a class with either of these keywords.

But what if I am writing my code into a class with inherited sharing, which may be consumed by classes which are both with and without sharing?

Is there any way to know in which mode the code is operating so that I could add conditional logic instead of having two completely separate implementations that might be largely identical?

2 Answers 2


This might not be the answer, but let me put my thoughts.

When you use inherited sharing it's the responsibility of the caller class to handling the sharing settings for the code which the called class will be executing.

Now, you say you want to add conditional logic based on sharing settings, this means you don't have trust in the caller, Thus I would say there is no point in using the inherited sharing.

I would go by having 2 separate classes which would specify the sharing context in name, now it would be callers responsibility to call appropriate classes. eg. MyLogicClassWithSharing , MyLogicClassWithoutSharing

If you worry about code duplication, I can think about putting the generic reusable code in some service method or putting it in a generic master class.

  • The problem with this approach is that there may be some long chain of dependencies between the initial invocation of some code and the code which would contain the conditional logic, so using two classes instead would require conditional logic to determine which class to invoke which would mean passing information all the way from the beginning of the chain. And if I were to do that, I might as well use that for my conditional logic in the class itself. May 22, 2019 at 21:27

I don't think there is. Although, Salesforce has introduced Inherited Sharing keyword, recently in their Winter 19 Release, to solve ambiguity issues associated with the with and without sharing Keywords.

You can read more about it here :


  • Cheers for the response, but the entire problem is that I want a class with inherited sharing to determine conditional logic based on the caller. May 22, 2019 at 21:24

You must log in to answer this question.

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