Saturday 23 March 2019

Inherited Sharing in Apex


Inherited Sharing is available with Salesforce Winter'19 release. Use Inherited sharing keyword on apex class which allows the class to run in sharing mode of class that called it. Inherited sharing ensures that our apex code is not used in unexpected or insecure way. An Apex class with inherited sharing runs as with sharing when used as a Visualforce page controller, Apex REST service, or an entry point to an Apex transaction.

If the class is used as the entry point to an Apex transaction, an omitted sharing declaration runs as without sharing. However, inherited sharing ensures that the default is to run as with sharing. A class declared as inherited sharing runs only as without sharing when explicitly called from an already established without sharing context.

Example:
We have declares Apex class with inherited sharing and a visualforce invocation of that Apex code. Because of the 'inherited sharing' declaration, only contacts for which the running user has access are displayed. If we removed inherited sharing then contacts that a user has no rights to view are displayed due to the insecure default behavior.


running user with owner of multiple records
running user owns only one contact record.

No comments:

Post a Comment