LoginSignup
5
4

More than 5 years have passed since last update.

Salesforce Apex 自分用メモ

Last updated at Posted at 2018-09-21

今週のコードで気になったことを忘れないようにメモ

関連先の項目までupdateしたいとき

object.anotherRef__r.customValue__c = something;
objectList.add(object);
update objectList;

↑だとうまくいかない
updateする対象がobjectなので、参照先まで更新されない
ので、参照先のオブジェクトのリストを作って更新するようにする

AnotherObject anotherObject = new AnotherObject();
anotherObject.Id = object.anotherRef__c;
anotherObject.customValue__c = something;

objectList.add(object);
anotherObjectList.add(anotherObject);
update objectList;
update anotherObjectList;

複数選択リスト項目への追加・更新

object.MultiSelect__c = object.MultiSelect__c + ';' + value;
update object; 

複数選択リストは;区切りの文字列らしいので、↑みたいにして追加して更新する

batch のテストをしたい時

Test.startTest();
hogeBatch batch = new hogeBatch();
Database.executebatch(batch);
Test.stopTest();

System.assertEquals(hoge, fuge);

Test.startTest();Test.stoptest();の間でバッチを実行させるとうまくいく
あとは必要に応じて、SOQLでテストしたいものを再取得すると良い

VFの selectOptionsの上限1000件を回避する方法

リストを分ける
<apex:apex:selectList value="{!country}" title="Choose a country">
  <apex:selectOptions value="{!items1}"/>
  <apex:selectOptions value="{!items2}"/>
</apex:selectList>

そもそも 1000件以下のリストしか渡さないようにした方が良いかも

LIMIT1000する
public List<SelectOption>  getItems() {
    List<SelectOption> items = new List<SelectOption>();
    List<Hoge> hogeList = new List<Hoge>();

    commodities = [SELECT Id, Name FROM Hoge LIMIT 1000];

    for (Hoge hoge : hogeList) {
        commodityCodes.add(new SelectOption(hoge.Id, hoge.Name));
    }

    return items;
}

余裕があれば、絞り込んだリストを selectOptions に渡すやつも書く

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4