function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Use of "Without Sharing" keyword on class

I am trying to understand if the "Without sharing" keyword will allow me do accomplish something I am trying to do with a VF page with a custom controller.
What my goal is, is to send a link to a base user that includes a link to my custom VF page, and include an id to an object that they would not normally have visibility to, for example, opportunities.
According to Premier support, "without sharing" executes under the System User, ignoring all CRUD, field level and row-level security. 
This would seem to do what I am trying to do.  However, I get an insufficient privileges error when a base user tries to access the page with an object id that they don't normally have access to.
Is this NOT with the "with sharing" or "without sharing" mean?  If it isn't, what is the purpose of these keywords and how are they used?
Have you given the access (VF Page) to that user.

Yes, the user can use the page to access some objects, but not all.

If the user can see the object (an Activity related to an opportunity) then the page is fine, but if they can't see the activity normally, then they get an error with the page.

I was hopeful that the "Without sharing" would allow me to pull the data back via apex dispite the users default visibility.

Anyone have any success with something like this?



Did you ever solve this?  I ran into this today and found something that worked for me.  The problem is that the controller runs "without sharing" but the page does not.


So in the controller, I select a bunch of fields with no issues and then expose (for instance) the opportunity to the page.  But the page grabs the opportunity row and then tries to get a field off of it and does not have permission because it is implicitly running with sharing.


I found two solutions.

(1) Create a method in the controller that just returns the column you want rather than the whole opportunity.  This is a hack but might be fine for quick-and-dirty if you want just one or two values.


(2) Go to Customize/Opportunites/Fields and click in to your field.  Click the "View Field Accessibility" button (not "Set Field-Level Security").  In the table, you'll find a bunch of columns with "Hidden."  Click on the right one and make it visible.  In this table you get all the profiles including guest license and your site profiles.  (My problem occurred with Sites.)  The other button only gives you the standard licenses.  (Why, Salesforce, why?)



-- Todd

soon-to-be Certified Advanced Developer


Thanks Shrini, Your suggestion works for me. :)

I have given Profile access to newly Created Page and its working fine now.