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
SKT CloudComputingSKT CloudComputing 

difference between with sharing and without sharing??

Hello All,

I am new to salesforce and gone through documentation. I ended with some queries as below-

1- What is default keyword?
2- If we do not write any keyword then what will happen?
3- If it run in by default mode then what is need for using 'without sharing' keyword.

Please provide some real exmaples (calling and called class format), it would be great help.


Thanks!


 
Amit Chaudhary 8Amit Chaudhary 8

Please check below post difference between sharing an without sharing
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm

2) http://salesforce.stackexchange.com/questions/11768/when-to-use-with-sharing-or-without-sharing
3)
The with sharing keyword allows you to specify that the sharing rules for the current user be taken into account for a class. You have to explicitly set this keyword for the class because Apex code runs in system context. In system context, Apex code has access to all objects and fields— object permissions, field-level security, sharing rules aren’t applied for the current user. This is to ensure that code won’t fail to run because of hidden fields or objects for a user. The only exceptions to this rule are Apex code that is executed with the executeAnonymous call and Chatter in Apex. executeAnonymous always executes using the full permissions of the current user

1- What is default keyword?

Reserved Keywords in salesforce. Please check below link
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_reserved_words.htm

If you are taking about access spec please check below post
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_access_modifiers.htm

2- If we do not write any keyword then what will happen?
If you are taking about class access modifirer then bu default it will without shaing

3- If it run in by default mode then what is need for using 'without sharing' keyword.
Use the without sharing keywords when declaring a class to ensure that the sharing rules for the current user are not enforced. For example, you may want to explicitly turn off sharing rule enforcement when a class acquires sharing rules when it is called from another class that is declared using with sharing

SKT CloudComputingSKT CloudComputing
Thanks Amit for reply.

1. here I meant to say the default keyword for with sharing and without sharing.

2. If we do not write with sharing or without sharing then what will be happen in case of-

a- if calling class is declared with 'with sharing' and called one doesn't have any keyword?
b- if calling class is declared with 'without  sharing' and called one doesn't have any keyword?

3- If we can achieve the purpose of not enforcing sharing rules by not writing any keywords(with or without sharing) then what is the need 
    for declaring it 'without sharing'.


Thanks!

 
farukh sk hdfarukh sk hd
This article has covered this in good way,

https://www.sfdc-lightning.com/2018/11/with-sharing-and-without-sharing-in.html

If we do not declare class with "With sharing" or "Without sharing"  the class will not take into account the sharing rules but if this class is called from another class which is declared with "With sharing" it will take into account the sharing rules.
IF the class with "With sharing" is calling method of another class with "Without sharing" than the method inside "Without sharing" class will execute without sharing rules.
IF the class with "Without sharing" is calling method of another class with "With sharing" than the method inside "With sharing" class will execute with sharing rules.
farukh sk hdfarukh sk hd