Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer
    
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 May 20 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). –  Ralph May 20 at 15:21
    
And I'm assuming that little added bit applies to VF pages as well? –  dphil May 20 at 15:23
    
That's correct, the context doesn't matter, VF, batch, email service, should all work the same. –  Ralph May 20 at 15:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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