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

環境
Salesforce Apex

構文
public with sharing class SharingTest
// 処理コードを記載
}

with sharingを使用した別のクラスから呼び出された場合、
そのクラスにもwith sharingが適用されます。
with 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;   
    }
}

 

IT

Posted by arkgame