Well, I got this new project which has a lot of customization in Apex classes. I was tasked to separate the complexity of a huge Controller class to a new Helper class. I know how to deal with the code, but I am wondering with the usage of: With/Without Sharing for the new class.

My question is: What all things to consider to come to a conclusion: whether to use With or Without sharing keywords to my new Helper class?

Note: Apex Controller class doesn't have with/without sharing.

This answer is really helpful in understanding most of my scenario, but I would like to get more insight.

1 Answer 1


It depends on the purpose of the class, but the usual answer is "neither." A utility class is likely to be called from a number of places, some that use sharing and others that don't. By not specifying a sharing model, you will assume the default behavior of whatever class calls your utility class. This is almost always the correct answer, since the calling class' security model should already be set appropriately.

There's also a minor overhead involved whenever you change a class boundary and have with sharing or without sharing specified (it updates the Apex Runtime's internal state, which takes a few milliseconds); by specifying either sharing model, you will cause extra governor limit (CPU time) to be wasted. The only time you should specify "with sharing" or "without sharing" is when you absolutely must have the utility class work in a particular model regardless of the calling context.

The most usual case when you'd want to do either would likely be "without sharing", because you need to do something and you need to guarantee the sharing model won't get in the way, like writing to a custom log object, or updating a hidden record. It's really, really unusual to need to do this, because most of the time sharing won't stop things like mere logging, but you always carefully think about adding either model to your code, because it's incredibly difficult to justify in a utility class.

  • Well said. I was thinking the same(not to this level). I don't want to ask this question to my Senior developer and get a bad impression on me :) Apr 2, 2018 at 21:27
  • 2
    @AustinEvans mind you, some people will likely disagree with this answer, but I've been doing this for a long time, so I've a sense of when I might need to do it, and there's always exceptions. In practice, it's better to set "with sharing" and be wrong, than to set either "without sharing" or default sharing and be wrong. Too little access you can fix, but too much can be a security risk.
    – sfdcfox
    Apr 2, 2018 at 23:02

You must log in to answer this question.

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