With sharing and without sharing in Apex Class

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.

Scenario 1:


<apex:page controller=”Sample”>
    The count is {!intCount}
</apex:page>


public with sharing class Sample {


    public Integer intCount {get;set;}
    
    public Sample() {
        intCount = [ SELECT COUNT() FROM Employee__c ];
    }
    
}


If OWD is Private, the VF will show no of Employee records the user have access to.
If OWD is Public Read Only or Public Read/Write, the VF will show no of Employee records in the employee object.


Scenario 2:


<apex:page controller=”Sample”>
    The count is {!intCount}
</apex:page>


public without sharing class Sample {


    public Integer intCount {get;set;}
    
    public Sample() {
        intCount = [ SELECT COUNT() FROM Employee__c ];
    }
    
}


If OWD is Private or Public Read Only or Public Read/Write, the VF will show no of Employee records in the employee object.

Scenario 3:


<apex:page controller=”Sample”>
    <apex:form >
        <apex:commandButton value=”Create Employee” action=”{!createEmp}”/>
    </apex:form>
</apex:page>


public with sharing class Sample {
    
    public Sample() {}
    
    public void createEmp() {
        Employee__c emp = new Employee__c();
        insert emp;
    }
    
}


If an user don’t have CRUD access to employee object and clicks Create Employee button, it will create employee record.


Scenario 4:


<apex:page controller=”Sample”>
    <apex:form >
        <apex:commandButton value=”Create Employee” action=”{!createEmp}”/>
    </apex:form>
</apex:page>


public without sharing class Sample {
    
    public Sample() {}
    
    public void createEmp() {
        Employee__c emp = new Employee__c();
        insert emp;
    }
    
}


If an user don’t have CRUD access to employee object and clicks Create Employee button, it will create employee record.

Scenario 5:


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:

The ‘without sharing’ keyword is to ensure that the sharing rules (not permissions) for the current user are not enforced.
The with sharing keyword allows you to specify that the sharing rules (and not permissions) for the current user be taken into account for a class. You have to explicitly set this keyword for the class because Apex code runs in system mode.

Leave a Reply