Sunday, December 30, 2018

Use of Inherited Sharing in Apex Class

This post explains the use of inherited sharing in apex class
If you specify inherited sharing Keyword on an Apex class,  which allows the class to run in the sharing mode of the class that called it. Using inherited sharing enables you to pass security review and ensure that your privileged Apex code is not used in unexpected or insecure ways.

  1. 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.
  2. An Apex class with Inherited Sharing is being called from some other class which is having without sharing setting, then it will run in without sharing mode.
This example declares an Apex class with inherited sharing and a Visualforce invocation of that Apex code. Because of the inherited sharing declaration, only opportunities for which the running user has sharing access are displayed. If the declaration is omitted, even opportunities that the user has no rights to view are displayed due to the insecure default behavior of omitting the declaration.
Apex Class
public inherited sharing class OpportunityMethods {
    public list<Opportunity> getAllTheOpps(){
        return [SELECT Id, Name, StageName FROM Opportunity];
Visualforce page
<apex:page controller="OpportunityMethods">
    <apex:repeat value="{!getAllTheOpps}" var="record">

Happy Learning!!

No comments:

Post a Comment