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
GauravTrivediGauravTrivedi 

By Default which sharing rule will apply on Apex??

In class declaration if we don’t write keyword “with sharing” then it runs in system mode then why keyword “without sharing” is introduced in apex?
Best Answer chosen by GauravTrivedi
bob_buzzardbob_buzzard
No, the default is to continue with the current sharing rule: 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_keywords_sharing.htm

All Answers

bob_buzzardbob_buzzard
Apex always runs in system mode, unless you run it from through the execute anonymous capability.

With/without sharing indicates whether the sharing rules for the currently logged in user should be applied when accessing records. The permissions of the currently logged in user are ignored regardless of with/without sharing, as the code is still running in system mode.
GauravTrivediGauravTrivedi
Thank you for your kind reply.

According to you by default it is with Sharing. 
bob_buzzardbob_buzzard
No, the default is to continue with the current sharing rule: 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_keywords_sharing.htm
This was selected as the best answer
Ashok RathvaAshok Rathva
Hi, Bob

  I have still doubt if Run in system mode then what is difference between without sharing & system mode. Please explain.?
 
  If any of (with/without) by default then why we have to write it or there may be difference between default and with/without..

Thanks
Ashok
bob_buzzardbob_buzzard
System mode is around permissions - it ignores the profile/permission set restrictions for the logged in user. With/without sharing is around sharing. With sharing means it will respect the sharing rules that apply to the current logged in user.

Thus running in system context means you can view sobject types that your profile doesn't allow, but with sharing stops you seeing any records that haven't been shared with yout.
Thilina DilharaThilina Dilhara
Without Sharing
"
By default Apex code executes without checking permissions. Hence the code will not enforce field level security, sharing rules and user permissions during execution of Apex code in Triggers, Classes and Controllers. This creates the risk that unauthorized users may get access to sensitive data records or fields.
To prevent this, developers should use with sharing keyword when declaring their classes if the class has SOQL or SOSL queries or DML Statements. This will ensure that current user’s permissions, field level security and sharing rules are enforced during code execution. Thus users will only see or modify records and fields which they have access to.
Use without sharing when a specific class should have full access to records without taking into account current user’s permissions. This should be used very carefully.
Use inherited sharing when the code should inherit the level of access from the calling class. This is more secure than not specifying a sharing level as the default will be equivalent to "with sharing".
"