I am getting confused when we need apex sharing. Sales-force has Sharing rules(criteria,owner-based,manual),so what is the use of Apex sharing. Somebody please provide usecase/example when we need apex sharing..


One example is where your org supports users from multiple 3rd party businesses where they should only see their own data as they are potentially competitors but you need to see all the data.

Sharing rules control access to table rows whereas profiles and permission sets control access to table columns (including all columns i.e. access to the whole table).

A valuable piece of documentation about the mechanics of the language feature is Using the with sharing or without sharing Keywords that makes these points:

  • The sharing setting of the class where the method is defined is applied, not of the class where the method is called. For example, 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.
  • If a class isn’t declared as either with or without sharing, the current sharing rules remain in effect. This means that the class doesn’t enforce sharing rules except if 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.
  • Both inner classes and outer classes can be declared as with sharing. The sharing setting applies to all code contained in the class, including initialization code, constructors, and methods.
  • Inner classes do not inherit the sharing setting from their container class.
  • Classes inherit this setting from a parent class when one class extends or implements another.
| improve this answer | |

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