Quantcast
Channel: tyoshikawa1106のブログ
Viewing all articles
Browse latest Browse all 1437

SFDC:StandardControllerのaddFieldsとApexテスト

$
0
0

standardControllerのaddFieldsを利用すればクエリを投げずに追加で項目を取得することが可能です。

private void SampleController(ApexPages.StandardController stdController) {
    stdController.addFields(new List<String> {'Sample__c', 'Sample__r.Name'});
}


便利な処理ですが、テストクラスで下記エラーが発生することがあります。

System.SObjectException: You cannot call addFields when the data is being passed into the controller by the caller.


エラーの発生はテストクラス内で次のように対象オブジェクトをnewするような処理を書いたときに発生します。

SampleController cls = new SampleController(new ApexPages.StandardController(new Demo__c()));


回避方法を検索したところ、Test.isRunnningTestを利用する方法になるみたいです。

if (!Test.isRunningTest()) { 
    stdController.addFields(new List<String> {'Sample__c', 'Sample__r.Name'});
}      

参考


Viewing all articles
Browse latest Browse all 1437

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>