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
Ashritha ReddyAshritha Reddy 

Please answer me

what is the with sharing and without sharing in apex ?can any one provide small example with brief description???
Best Answer chosen by Ashritha Reddy
Lalit Karamchandani 27Lalit Karamchandani 27
  • public with sharing class A {}
  • public without sharing class B{}
  • public class C{} // Class C is a non-specified-sharing class.
Now, let's consider the following case scenarios:
  1. class B extends A // class B's code now executes in class A's mode, i.e. in "with sharing" mode.
  2. class B calls class A // called code will now be executed in the mode of the class in which it was defined, in this case, in class A's mode, i.e. in "with sharing" mode.
  3. class C calls class A // called method's code in class A will execute in class A's mode, i.e. in "with sharing" mode.
  4. class C extends class A // code in class C now executes in the parent class A's mode, i.e. in "with sharing" mode.
  5. class A calls C // code in class C is executed in class C's mode, i.e. in "without sharing" mode although it's sharing settings are unspecified in the class' declaration.
  6. class A extends C // class A's code now executes in the parent class C's mode, i.e. in "without sharing" mode.
public with sharing class sharingClass {

// Code here

}
public without sharing class noSharing {

// Code here

}

More Info:- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm
 

All Answers

CloudGeekCloudGeek
Hello,
 
If not declared explicitly, CLASSES are always With Sharing.

It is important to understand that in both cases (With or Without Sharing), Apex runs in System Mode, so it wouldn't respect things like Field Level Security.

Use With Sharing when the User has access to the records being updated via Role, Sharing Rules, Sales Teams - any sort of sharing really.

Without Sharing is reserved for cases where the User does not have access to the records, but there is a need to update them based on user input.

Also the With / Without Sharing commutes and if a class doesn't have an explicit declaration, the sharing context of the calling class is inherited.


Note: Mark this as solution if this helps!
Ashritha ReddyAshritha Reddy
I need some more clarification!!
CloudGeekCloudGeek
Okay.

Default - Without Sharing

Now, Apex class always execute in system context i.e. Apex code has access to all objects and fields irrespective of the logged in User.

Example - lets consider you have VF page in which you are showing certain fields as columns. Lets see one column says "Sales Rep Performance" which displays a flag in red, green and yellow. Now ideally this field should not be visible whenever a Sales Rep accesses this page (consider this as business requirement).
But it is always visible if the class has no keyword specified or if a class has without sharing specified.
Now once the class is "with sharing" the object permissions, field-level security, sharing rules are applied for the current user and fields which should not be visible/accessible and not visible or accessible.

Important -
  1. If a method is defined in a class declared with 'with sharing' is called by a class declared with 'without sharing', the method will execute with sharing rules enforced.
  2. The class doesn’t enforce sharing rules except if it acquires sharing rules from another class. Ex. Class A (with sharing) calls a method from Class B(Without sharing) then complete context is 'with sharing'
  3. Inner classes do not inherit the sharing setting from their container class.

Hope this gives good understanding ?
 
Lalit Karamchandani 27Lalit Karamchandani 27
  • public with sharing class A {}
  • public without sharing class B{}
  • public class C{} // Class C is a non-specified-sharing class.
Now, let's consider the following case scenarios:
  1. class B extends A // class B's code now executes in class A's mode, i.e. in "with sharing" mode.
  2. class B calls class A // called code will now be executed in the mode of the class in which it was defined, in this case, in class A's mode, i.e. in "with sharing" mode.
  3. class C calls class A // called method's code in class A will execute in class A's mode, i.e. in "with sharing" mode.
  4. class C extends class A // code in class C now executes in the parent class A's mode, i.e. in "with sharing" mode.
  5. class A calls C // code in class C is executed in class C's mode, i.e. in "without sharing" mode although it's sharing settings are unspecified in the class' declaration.
  6. class A extends C // class A's code now executes in the parent class C's mode, i.e. in "without sharing" mode.
public with sharing class sharingClass {

// Code here

}
public without sharing class noSharing {

// Code here

}

More Info:- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm
 
This was selected as the best answer