With sharing and without sharing in Apex Class

With sharing and without sharing in Apex Class determines whether the class should enforce all the Organization sharing settings.

Example:

Visualforce page:

<apex:page sidebar="false" controller="Sample">
    <apex:form >
       <apex:pageblock >
            <apex:pageblockTable value="{!var}" var="a">
                <apex:column value="{!a.Name}"/>
            </apex:pageblockTable>
        </apex:pageblock>   
    </apex:form>
</apex:page>


Apex Class:

public with Sharing class Sample
{
    public List<Account> getVar()
    {
        return [SELECT Name FROM Account];
    }    
}


OWD for Account is Private.



Role Hierarchy:


Output of the page for Managing Director Role:




Output of the page for Manager Role:

No comments:

Post a Comment