+ 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.