3

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?

6

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. – Brian Kessler May 22 at 21:27
0

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 :

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm

  • 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. – Brian Kessler May 22 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.