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
Nandhini s 11Nandhini s 11 

with sharing and without sharing in apex

Hi guys,

I know 'with sharing' enforces sharing rules of the current user and 'Without sharing' doesn't.
My doubt is, what is sharing rules? is it the criteria/owner based sharing rule we write for an object? or does it mean the access a user has on an object?

Where do we use these keywords in real time?
Is the current user, the user who executes the code?
pconpcon
The sharing rules are based on the user that is running the apex code.  If that's a trigger, whoever inserted the object.  If it's a Visualforce controller, whoever is viewing the page.  If it's a Apex REST/SOAP endpoint, whoever is making the API request.

The keywords are used at the class level definition.  This is most common when writing Visualforce controllers or API endpoints.  However, you can use them in triggers to bypass sharing rules if needed.