<!-- VF Page -->
<apex:page controller="MainClass">
lstAccountWithoutSharing:<br/>
{!lstAccountWithoutSharing}<br/><br/>
lstAccountWithSharing:<br/>
{!lstAccountWithSharing}<br/><br/>
</apex:page>
//With sharing and without sharing when calling to other class
public with sharing class MainClass
{
public List<Account> lstAccountWithoutSharing{get;set;}
public List<Account> lstAccountWithSharing{get;set;}
public MainClass()
{
lstAccountWithoutSharing=new List<Account>();
lstAccountWithSharing=new List<Account>();
lstAccountWithoutSharing=AccountsWithoutSharing.getAccountsWithoutSharing();
lstAccountWithSharing=AccountsWithSharing.getAccountsWithSharing();
}
}--------------------------------------------------
public without sharing class AccountsWithoutSharing
{
public static List<Account> getAccountsWithoutSharing()
{
return [Select id,name from Account];
}
}
--------------------------------------------------
public with sharing class AccountsWithSharing
{
public static List<Account> getAccountsWithSharing()
{
return [Select id,name from Account];
}
}
-----------------------OUTPUT-----------------------------
lstAccountWithoutSharing:
[0010I00001ZFJgRQAX, 0010I00001ZFJhPQAX, 0010I00001ZFJh0QAH, 0012800001Yi6zXAAR, 0012800001Yi6xbAAB, 0012800001Yi74IAAR, 0010I00001ZFJiYQAX, 0012800001Yi6HhAAJ, 0010I00001ZFJh5QAH, 0012800001YhpexAAB, 0010I00001ZFJhFQAX, 0010I00001ZFJhKQAX, 0010I00001ZFJglQAH, 0010I00001ZFJgvQAH, 0012800001Yi71sAAB, 0012800000uVGpTAAW, 0012800000uVE7qAAG, 0012800000uVD7iAAG, 0012800000uVGkMAAW, 0012800001Yi73yAAB, 0012800001Yi6LAAAZ, 0012800001Yi6yPAAR, 0012800001Yi6zTAAR, 0012800001Yi71nAAB, 0012800001Yi6zSAAR, 0012800001Yi75cAAB, 0012800001YiKBTAA3, 0012800001Yi6JsAAJ, 0012800001Yi6JtAAJ, 0012800001Yi6JuAAJ, 0012800001Yi6JvAAJ, 0012800001Yi75XAAR, 0012800001Yi76QAAR, 0012800001Yi78HAAR, 0010I00001dvdJIQAY, 0010I00001dvdJcQAI, 0010I00001dvdMnQAI, 0010I00001dvdMdQAI, 0010I00001dvdJDQAY, 0010I00001dvdJXQAY, 0010I00001dvdJ3QAI, 0010I00001cyy8wQAA, 0012800001Yi6JxAAJ, 0012800001Yi76BAAR, 0012800001Yi75SAAR, 0012800001YiKOwAAN, 0012800001YhpNhAAJ, 0012800001YiKZVAA3, 0012800000tnBmuAAE, 0012800000tnBorAAE, 0012800000tnB0sAAE, 0012800000tnB0qAAE, 0012800000tnB0rAAE, 0012800000tnB0iAAE, 0012800000tnB0jAAE, 0012800000tnB0kAAE, 0012800000tnB0lAAE, 0012800000tnB0mAAE, 0012800000tnB0oAAE, 0012800000tnB0pAAE, 0012800000tnB0nAAE, 0012800000tnB0tAAE]
lstAccountWithSharing:
[]
----------------------------------------------
Conclusion:
Result is not dependent on what the sharing contaxt of Main Class it depends on what the sharing contaxt of the class in which SOQL is executing
Lets do some more exercise:
------------------------------------------------------------------------
create an other class:
public virtual without sharing class WithoutSharingContaxt
{
}
-------------
and
-------------
public with sharing class AccountsWithSharing extends WithoutSharingContaxt
{
public static List<Account> getAccountsWithSharing()
{
return [Select id,name from Account];
}
}
------------------------------
then lets check the output
-----------------------OUTPUT-----------------------------
lstAccountWithoutSharing:
[0010I00001ZFJgRQAX, 0010I00001ZFJhPQAX, 0010I00001ZFJh0QAH, 0012800001Yi6zXAAR, 0012800001Yi6xbAAB, 0012800001Yi74IAAR, 0010I00001ZFJiYQAX, 0012800001Yi6HhAAJ, 0010I00001ZFJh5QAH, 0012800001YhpexAAB, 0010I00001ZFJhFQAX, 0010I00001ZFJhKQAX, 0010I00001ZFJglQAH, 0010I00001ZFJgvQAH, 0012800001Yi71sAAB, 0012800000uVGpTAAW, 0012800000uVE7qAAG, 0012800000uVD7iAAG, 0012800000uVGkMAAW, 0012800001Yi73yAAB, 0012800001Yi6LAAAZ, 0012800001Yi6yPAAR, 0012800001Yi6zTAAR, 0012800001Yi71nAAB, 0012800001Yi6zSAAR, 0012800001Yi75cAAB, 0012800001YiKBTAA3, 0012800001Yi6JsAAJ, 0012800001Yi6JtAAJ, 0012800001Yi6JuAAJ, 0012800001Yi6JvAAJ, 0012800001Yi75XAAR, 0012800001Yi76QAAR, 0012800001Yi78HAAR, 0010I00001dvdJIQAY, 0010I00001dvdJcQAI, 0010I00001dvdMnQAI, 0010I00001dvdMdQAI, 0010I00001dvdJDQAY, 0010I00001dvdJXQAY, 0010I00001dvdJ3QAI, 0010I00001cyy8wQAA, 0012800001Yi6JxAAJ, 0012800001Yi76BAAR, 0012800001Yi75SAAR, 0012800001YiKOwAAN, 0012800001YhpNhAAJ, 0012800001YiKZVAA3, 0012800000tnBmuAAE, 0012800000tnBorAAE, 0012800000tnB0sAAE, 0012800000tnB0qAAE, 0012800000tnB0rAAE, 0012800000tnB0iAAE, 0012800000tnB0jAAE, 0012800000tnB0kAAE, 0012800000tnB0lAAE, 0012800000tnB0mAAE, 0012800000tnB0oAAE, 0012800000tnB0pAAE, 0012800000tnB0nAAE, 0012800000tnB0tAAE]
lstAccountWithSharing:
[]
------------------------------------------------------
Conclusion:
HA HA what the difference here!
If current class contaxt is forced to with sharing we can not extend its contaxt to without sharing
Lets remove that forced with sharing keyword
--------------------------------------
public class AccountsWithSharing extends WithoutSharingContaxt
{
public static List<Account> getAccountsWithSharing()
{
return [Select id,name from Account];
}
}
-----------------
and see the output
-----------------------OUTPUT-----------------------------
lstAccountWithoutSharing:
[0010I00001ZFJgRQAX, 0010I00001ZFJhPQAX, 0010I00001ZFJh0QAH, 0012800001Yi6zXAAR, 0012800001Yi6xbAAB, 0012800001Yi74IAAR, 0010I00001ZFJiYQAX, 0012800001Yi6HhAAJ, 0010I00001ZFJh5QAH, 0012800001YhpexAAB, 0010I00001ZFJhFQAX, 0010I00001ZFJhKQAX, 0010I00001ZFJglQAH, 0010I00001ZFJgvQAH, 0012800001Yi71sAAB, 0012800000uVGpTAAW, 0012800000uVE7qAAG, 0012800000uVD7iAAG, 0012800000uVGkMAAW, 0012800001Yi73yAAB, 0012800001Yi6LAAAZ, 0012800001Yi6yPAAR, 0012800001Yi6zTAAR, 0012800001Yi71nAAB, 0012800001Yi6zSAAR, 0012800001Yi75cAAB, 0012800001YiKBTAA3, 0012800001Yi6JsAAJ, 0012800001Yi6JtAAJ, 0012800001Yi6JuAAJ, 0012800001Yi6JvAAJ, 0012800001Yi75XAAR, 0012800001Yi76QAAR, 0012800001Yi78HAAR, 0010I00001dvdJIQAY, 0010I00001dvdJcQAI, 0010I00001dvdMnQAI, 0010I00001dvdMdQAI, 0010I00001dvdJDQAY, 0010I00001dvdJXQAY, 0010I00001dvdJ3QAI, 0010I00001cyy8wQAA, 0012800001Yi6JxAAJ, 0012800001Yi76BAAR, 0012800001Yi75SAAR, 0012800001YiKOwAAN, 0012800001YhpNhAAJ, 0012800001YiKZVAA3, 0012800000tnBmuAAE, 0012800000tnBorAAE, 0012800000tnB0sAAE, 0012800000tnB0qAAE, 0012800000tnB0rAAE, 0012800000tnB0iAAE, 0012800000tnB0jAAE, 0012800000tnB0kAAE, 0012800000tnB0lAAE, 0012800000tnB0mAAE, 0012800000tnB0oAAE, 0012800000tnB0pAAE, 0012800000tnB0nAAE, 0012800000tnB0tAAE]
lstAccountWithSharing:
[0010I00001ZFJgRQAX, 0010I00001ZFJhPQAX, 0010I00001ZFJh0QAH, 0012800001Yi6zXAAR, 0012800001Yi6xbAAB, 0012800001Yi74IAAR, 0010I00001ZFJiYQAX, 0012800001Yi6HhAAJ, 0010I00001ZFJh5QAH, 0012800001YhpexAAB, 0010I00001ZFJhFQAX, 0010I00001ZFJhKQAX, 0010I00001ZFJglQAH, 0010I00001ZFJgvQAH, 0012800001Yi71sAAB, 0012800000uVGpTAAW, 0012800000uVE7qAAG, 0012800000uVD7iAAG, 0012800000uVGkMAAW, 0012800001Yi73yAAB, 0012800001Yi6LAAAZ, 0012800001Yi6yPAAR, 0012800001Yi6zTAAR, 0012800001Yi71nAAB, 0012800001Yi6zSAAR, 0012800001Yi75cAAB, 0012800001YiKBTAA3, 0012800001Yi6JsAAJ, 0012800001Yi6JtAAJ, 0012800001Yi6JuAAJ, 0012800001Yi6JvAAJ, 0012800001Yi75XAAR, 0012800001Yi76QAAR, 0012800001Yi78HAAR, 0010I00001dvdJIQAY, 0010I00001dvdJcQAI, 0010I00001dvdMnQAI, 0010I00001dvdMdQAI, 0010I00001dvdJDQAY, 0010I00001dvdJXQAY, 0010I00001dvdJ3QAI, 0010I00001cyy8wQAA, 0012800001Yi6JxAAJ, 0012800001Yi76BAAR, 0012800001Yi75SAAR, 0012800001YiKOwAAN, 0012800001YhpNhAAJ, 0012800001YiKZVAA3, 0012800000tnBmuAAE, 0012800000tnBorAAE, 0012800000tnB0sAAE, 0012800000tnB0qAAE, 0012800000tnB0rAAE, 0012800000tnB0iAAE, 0012800000tnB0jAAE, 0012800000tnB0kAAE, 0012800000tnB0lAAE, 0012800000tnB0mAAE, 0012800000tnB0oAAE, 0012800000tnB0pAAE, 0012800000tnB0nAAE, 0012800000tnB0tAAE
------------------------------------------------------
Conclusion:
HA HA what the difference here!
we can extend the current class contaxt to without sharing only if no sharing contaxt(with sharing or without sharing) is specified
Hope with and without sharing thing is more to understand here!
Cheers!✍...