I have one checkbox on my vf page.when i select this i want that the class to which this VF page is referring should work in with sharing mode and when i deselect it class should work in without sharing mode.Any suggestions how can i implement this functionality?
This is little tricky. So what can you do is keep the logic that needs to be executed in a Util Class. Say
Now define two more classes
Now define your controller and from controller depending on the checkbox checked call the corresponding class
PS: The code is written in Notepad my not actually compile. Please feel free to make changes
UPDATE : Just realized you dont even need the second class(without sharing) just call the util class directly from the controller.
The following solution does not work:
Put the functionality in a separate virtual class (with sharing), and create a subclass which is annotated "without sharing". You would choose which one to instantiate in your controller.
It appears that in a class hierarchy, the platform respects the sharing annotation of the class in which the method is defined, and not necessarily the class that is instantiated. For example:
If I construct an instance of AccountSelectorWithoutSharing, and call selectAccounts(String), the method is executed "with sharing" because of the annotation in the class where the method is defined (AccountSelector).
The fact that the class I have instantiated, AccountSelectorWithoutSharing, is "without sharing" has no influence.
I've found the simplest way to do this is to have the parent class run in with sharing mode and then use a util class to perform certain tasks in system mode.
Example Parent Class
My Sudo Helper class looks like this