Apex without sharingの使い方のサンプル

環境
salesforce

構文
public without sharing class SharingTest
// 処理コードを記載
}
without sharingはシステムモードで実行するため、共有ルールは適用されません。

操作例
1.Apex側コード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public without sharing class ShareSample {
List<Account> accList{get;set;}
public ShareSampe(){
}
public List<Account>getAccList(){
List<Account>resList;
//リストの取得
resList = [SELECT Id,Name,Owner.Name FROM Account
ORDER BY Owner.Name ASC];
return resList;
}
}
public without sharing class ShareSample { List<Account> accList{get;set;} public ShareSampe(){ } public List<Account>getAccList(){ List<Account>resList; //リストの取得 resList = [SELECT Id,Name,Owner.Name FROM Account ORDER BY Owner.Name ASC]; return resList; } }
public without sharing class ShareSample {

  List<Account> accList{get;set;}
  
  public ShareSampe(){
  }
  
  public List<Account>getAccList(){
  
    List<Account>resList;
    //リストの取得
    resList = [SELECT Id,Name,Owner.Name FROM Account
    
              ORDER BY Owner.Name ASC];

     return resList;
  }

}

2.Visualforceコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<apex:page controller="ShareSample">
<apex:pageBlock>
件数: {!accList.size}
<apex:pageBlocktable value="{!accList}" var="cft">
<apex:column value="{!cft.id}"/>
<apex:column value="{!cft.Name}"/>
<apex:column value="{!cft.Owner.Name}"/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
<apex:page controller="ShareSample"> <apex:pageBlock> 件数: {!accList.size} <apex:pageBlocktable value="{!accList}" var="cft"> <apex:column value="{!cft.id}"/> <apex:column value="{!cft.Name}"/> <apex:column value="{!cft.Owner.Name}"/> </apex:pageBlockTable> </apex:pageBlock> </apex:page>
<apex:page controller="ShareSample">
  <apex:pageBlock>
   件数: {!accList.size}
   <apex:pageBlocktable value="{!accList}" var="cft">
       <apex:column value="{!cft.id}"/>
       <apex:column value="{!cft.Name}"/>
       <apex:column value="{!cft.Owner.Name}"/>
    </apex:pageBlockTable>
   </apex:pageBlock>

</apex:page>

 

Apex

Posted by arkgame