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

with sharing without sharing ???????????????

with sharing with out sharing, Which is the default in Apex class?????????


please help me....

Rakesh BoddepalliRakesh Boddepalli

Hi N@ni..


With sharing is the default in Apex class.

and it is recommended to always use with sharing as this adheres to the salesforce FLS and sharing rules. It will throw an error if you try to do any DML operations as part with out acess.


using without sharing , runs the code in the system code and doesnt obey any security settings.


If this answers your question , Pleae mark this as solution and give kudos under my Name.

Tim BarsottiTim Barsotti

If you do not specify "With Sharing" or "Without Sharing" it will execute in system context. 



Apex generally runs in system context; that is, the current user's permissions, field-level security, and sharing rules aren’t taken into account during code execution.




Both responders are incorrect.

Apex controllers will use 'with sharing' by default BUT Apex classes won't. Apex classes run in the context they were called from.

Case 1: Apex Controller called from VF page. If you don't specify it will run as 'with sharing' because the calling context ist he page.
Case 2: Apex class called from a Trigger. This will run as 'without sharing' because the calling class uses System permissions.

Tim BarsottiTim Barsotti

dphill - please link to documentation for this. I am not seeing what your are referring to here. 


"Like other Apex classes, all controllers and controller extensions run in system mode. Typically, you want a controller or controller extension to respect a user's organization-wide defaults, role hierarchy, and sharing rules. You can do that by using the with sharing keywords in the class definition."


"Apex generally runs in system context; that is, the current user's permissions, field-level security, and sharing rules aren’t taken into account during code execution."


Tim is correct.  Both APEX controllers and classes run in system mode by default (ignore sharing permissions).  I think the reason this question confuses a lot of developers is because, if you don't specify "without sharing" in your class, and a class that your trigger or class calls/runs DOES have "with sharing" set, your class will use the "with sharing" settings of that other class unless you specifically write "without sharing" in *your* class to override that heirarchical behavior.  As a result, that IF/ELSE IF/ELSE IF system behavior tends to confuse a lot of developers if they haven't actually read how the system determines whether or not to ignore a users sharing permissions while running your class.


By the way, I like that you use the developers handbook Tim.  All these answers are in there and super easy to find, but it seems like most people respond on here with what they "think", instead of taking 15 seconds to open their handbooks in order to "be sure".  :)


Sorry for taking so long to get back.  This line specifically.  I have tested this.


If a class is not declared as either with or without sharing, the current sharing rules remain in effect. This means that if the class is called by a class that has sharing enforced, then sharing is enforced for the called class.



farukh sk hdfarukh sk hd