tyoshikawa1106のブログ

- Force.com Developer Blog -

SFDC:Spring'17の選択リスト型項目とAPI参照名

Spring'17から選択リスト項目にAPI名という仕組みが追加されました。
f:id:tyoshikawa1106:20170129190502p:plain


一意のAPI名を設定することでリスト値の表示を変更してもApex側に影響がでないようにするための仕組みだと思います。次のようにリスト項目にAPI名をセットしてINSERTすれば登録処理を実行できます。
f:id:tyoshikawa1106:20170129190641p:plain


ボタンをクリックしてINSERTを実行すると・・・
f:id:tyoshikawa1106:20170129191219p:plain


選択リスト項目にAPI名に紐付くラベルが登録されます。
f:id:tyoshikawa1106:20170129191317p:plain


もしAPI名ではなくラベルをセットしてINSERTすると・・・
f:id:tyoshikawa1106:20170129191437p:plain


下記のエラーとなります。
f:id:tyoshikawa1106:20170129191529p:plain

Insert failed. First exception on row 0; first error: INVALID_OR_NULL_FOR_RESTRICTED_PICKLIST, ~ : 制限つき選択リスト項目の値が不適切


このエラーの回避方法として「値セットで定義された値に選択リストを制限します。」のチェックをはずすとラベル値の指定でも登録できました。
f:id:tyoshikawa1106:20170129191712p:plain

f:id:tyoshikawa1106:20170129191748p:plain


選択リストのAPI名ですがユーザが自由に変更できないように設定することも可能みたいです。
f:id:tyoshikawa1106:20170129191837p:plain


Spring'17は2017年2月にリリースされます。現在のWinter'17では利用できない機能です。Spring'17になっているSandbox側で対応してWinter'17にリリースしてしまうとエラーになってしまうと思います。またバージョンアップ時に既存機能に影響が出やすい場所だと思いますので注意してチェックしておいた方が良さそうです。もしこの機能周りでトラブルに遭遇したら「値セットで定義された値に選択リストを制限します。」のチェックを外して応急処置できると思います。

追記

API参照名ですがデフォルトで日本語がセットされると思います。これは一時的にセットされるもので本格的に利用する場合は英数字で指定して利用する仕様となっています。導入時には必ず英数字に指定してから利用を開始しましょう。
f:id:tyoshikawa1106:20170129201034p:plain

サンプルコード

検証用のサンプルコードです。APIバージョンは39.0で利用できます。

<apex:page controller="SampleVFController" showHeader="true" sidebar="false">
    <apex:slds rendered="true" />
    <div class="slds">
        <div class="slds-m-around--small">
            <apex:form id="form">
                <apex:commandButton value="よい" styleClass="slds-button slds-button--brand" action="{!doSaveType1}" reRender="form" />
                &nbsp;
                <apex:commandButton value="ふつう" styleClass="slds-button slds-button--brand" action="{!doSaveType2}" reRender="form" />
                &nbsp;
                <apex:commandButton value="わるい" styleClass="slds-button slds-button--brand" action="{!doSaveType3}" reRender="form" />
            </apex:form>
        </div>
    </div>
</apex:page>
public with sharing class SampleVFController {

    public Sample__c sample {get; set;}

    /**
     * コンストラクタ
     */
    public SampleVFController() {
        this.sample = new Sample__c();
    }

    /**
     * 保存処理①
     */
    public PageReference doSaveType1() {
        this.sample.SampleList__c = 'よい';
        insert sample;
        return new PageReference('/' + this.sample.Id);
    }

    /**
     * 保存処理②
     */
    public PageReference doSaveType2() {
        this.sample.SampleList__c = 'Normal';
        insert sample;
        return new PageReference('/' + this.sample.Id);
    }

    /**
     * 保存処理③
     */
    public PageReference doSaveType3() {
        this.sample.SampleList__c = 'Bad';
        insert sample;
        return new PageReference('/' + this.sample.Id);
    }
}