0

I am bit confused about with sharing and without sharing in Apex. I have set OWD to private for Contacts and created sharing rules. If I login as an Admin, I see all the contacts. If I login with a non admin user(NA) with a particluar role, I see contacts created by this user and contacts created by other users but with NA user's role (because of sharing settings). So if in an apex class with omitted sharing (which behaves as without sharing) I query for contacts I will only see limited number of contacts if the user who is calling this class is NA (I tested using a test class with seeAllData=true and system.runAs with NA user). So my question is what is the point of with/without sharing as in my case even with omitted sharing, it seems to work as though it is working as with sharing. It says here that

The with sharing keyword allows you to specify that the sharing rules for the current user are considered for the class. You have to explicitly set this keyword for the class because Apex code runs in system context.

but also says in the Implementation Details About with sharing and without sharing Keywords section:

If a class isn’t declared as either with or without sharing, the current sharing rules remain in effect.

1

No. Default behavior when you do not specify sharing is inherited sharing. From the Apex Developer Guide

If a class isn’t declared as either with or without sharing, the current sharing rules remain in effect. Therefore, the class doesn’t enforce sharing rules except when it acquires sharing rules from another class. For example, if the class is called by another class that has sharing enforced, then sharing is enforced for the called class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.