Salesforce with sharingとwithout sharingの使い方

概要
Without Sharing
現在のユーザーに適用されている共有ルールを強制実行されないようにする
には、クラスの宣言時に without sharing キーワードを使用します。
without sharingはシステムモードで実行するため、共有ルールは適用されません。
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public without sharing class SharingTest{
List<Account> accList{get;set;}
public SharingTest(){
}
public List<Account> getAccList(){
List<Account> result;
result = [SELECT Id,Name,Owner.Name
FROM Account
ORDER BY Owner.Name ASC
];
return result;
}
}
public without sharing class SharingTest{ List<Account> accList{get;set;} public SharingTest(){ } public List<Account> getAccList(){ List<Account> result; result = [SELECT Id,Name,Owner.Name FROM Account ORDER BY Owner.Name ASC ]; return result; } }
public without sharing class SharingTest{
   
   List<Account> accList{get;set;}
   
   public SharingTest(){
   }
   
   public List<Account> getAccList(){
     
       List<Account> result;
       
       result = [SELECT Id,Name,Owner.Name
                 FROM Account
                     ORDER BY Owner.Name ASC
                     ];
       return result;
   }
}

使用例2
with sharingだとユーザモードでの実行となるため、共有ルールが適用されます。
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public with sharing class SharingTest{
List<Account> accList{get;set;}
public SharingTest(){
}
public List<Account> getAccList(){
List<Account> result;
result = [SELECT Id,Name,Owner.Name
FROM Account
ORDER BY Owner.Name ASC
];
return result;
}
}
public with sharing class SharingTest{ List<Account> accList{get;set;} public SharingTest(){ } public List<Account> getAccList(){ List<Account> result; result = [SELECT Id,Name,Owner.Name FROM Account ORDER BY Owner.Name ASC ]; return result; } }
public with sharing class SharingTest{
   
   List<Account> accList{get;set;}
   
   public SharingTest(){
   }
   
   public List<Account> getAccList(){
     
       List<Account> result;
       
       result = [SELECT Id,Name,Owner.Name
                 FROM Account
                     ORDER BY Owner.Name ASC
                     ];
       return result;
   }
}

Visualforce画面

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

 

IT

Posted by arkgame