2

Currently, I have a little VF report that certain users can run. If the controller is specified as with sharing and it queries for data elsewhere that uses without sharing and returns it, can I display the data that was queried on the VF page or will it automatically revert the returned records to use the sharing on it?

Does this make sense or do I need to add some code for context?

1 Answer 1

4

To determine the sharing mode for an apex class the system first looks for a sharing declaration, e.g. with or without sharing, and uses that. If it's not found it inherits the sharing declaration from the caller.

So in your case, yes, that would do what you want.

From the docs

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.

4
  • Thanks. I'm going to do a little further testing just to be safe before deploying to production before I accept it, but for now I'll assume it works like this.
    – dphil
    Commented May 20, 2014 at 15:12
  • Always a good idea, but this is a pretty standard pattern. It's used a lot in managed packages to pass security review (basically everything should be with sharing then you delegate to a "sudo" class with a without sharing declaration to do queries in system mode). Commented May 20, 2014 at 15:21
  • And I'm assuming that little added bit applies to VF pages as well?
    – dphil
    Commented May 20, 2014 at 15:23
  • That's correct, the context doesn't matter, VF, batch, email service, should all work the same. Commented May 20, 2014 at 15:25

You must log in to answer this question.

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