public class C{} // Class C is a non-specified-sharing class.
Now, let's consider the following case scenarios:
class B extends A // class B's code now executes in class A's mode, i.e. in "with sharing" mode.
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.
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.
class C extends class A // code in class C now executes in the parent class A's mode, i.e. in "with sharing" mode.
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.
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
}
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.
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 -
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.
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'
Inner classes do not inherit the sharing setting from their container class.
public class C{} // Class C is a non-specified-sharing class.
Now, let's consider the following case scenarios:
class B extends A // class B's code now executes in class A's mode, i.e. in "with sharing" mode.
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.
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.
class C extends class A // code in class C now executes in the parent class A's mode, i.e. in "with sharing" mode.
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.
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
}
- 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:More Info:- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm
All Answers
Note: Mark this as solution if this helps!
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 -
Hope this gives good understanding ?
- 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:More Info:- https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm