この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

switch ステートメント

式が複数の値のいずれかと一致するかどうかをテストし、それに応じて分岐する switch ステートメントが Apex で提供されます。

構文は次のとおりです。

1switch on expression {
2    when value1 {		// when block 1
3        // code block 1
4    }	
5    when value2 {		// when block 2
6        // code block 2
7    }
8    when value3 {		// when block 3
9        // code block 3
10    }
11    when else {		  // default block, optional
12        // code block 4
13    }
14}

when の値は、1 つの値、複数の値、または sObject 型のいずれかになります。たとえば、次のように指定します。

1when value1 {
2}
1when value2, value3 {
2}
1when TypeName VariableName {
2}

switch ステートメントでは、式が評価されて、一致する when の値のコードブロックが実行されます。一致する値がない場合、when else コードブロックが実行されます。when else ブロックがない場合、アクションは実行されません。

フォールスルーはありません。コードブロックが実行されると、switch ステートメントは終了します。

メモ

Apex switch ステートメントの式では、次のいずれかの型になります。
  • Integer
  • Long
  • sObject
  • String
  • Enum

when ブロック

when ブロックには、式と照合する値が含まれます。これらの値には、次のいずれかの形式を使用できます。

  • when literal {} (when ブロックに複数のカンマ区切りのリテラル句を使用可能)
  • when SObjectType identifier {}
  • when enum_value {}

null はすべての型で有効な値です。

when 値は一意である必要があります。たとえば、リテラル x は 1 つの when ブロック句でのみ使用できます。when ブロックは 1 回のみ照合されます。

when else ブロック

式に一致する when 値がない場合、when else ブロックが実行されます。

必須ではありませんが、特に列挙型を使用する場合は when else ブロックを含めることをお勧めします。管理パッケージで提供される列挙値を使用して switch ステートメントを作成すると、パッケージの新しいバージョンに追加の列挙値が含まれる場合は、コードが適切に動作しないことがあります。この問題を回避するには、予期しない値を処理する when else ブロックを含めます。

メモ

when else ブロックを含める場合、switch ステートメントの最後のブロックにする必要があります。

リテラルでの例

リテラル when 値を使用して、Integer、Long、String 型を切り替えることができます。文字列の句は大文字と小文字を区別します。たとえば、「orange」は「ORANGE」とは異なる値です。

単一値の例

次の例では、when 値に整数リテラルを使用します。

1switch on i {
2   when 2 {
3       System.debug('when block 2');
4   }
5   when -3 {
6       System.debug('when block -3');
7   }
8   when else {
9       System.debug('default');
10   }
11}
null 値の例

Apex のすべての型は null にすることができるため、when 値を null にできます。

1switch on i {
2   when 2 {
3       System.debug('when block 2');
4   }
5   when null {
6       System.debug('bad integer');
7   }
8   when else {
9       System.debug('default ' + i);
10   }
11}
複数値の例

Apex switch ステートメントはフォールスルーしませんが、when 句に照合する複数のリテラル値を含めることができます。Apex switch ステートメントをネストして、when 句内で複数の実行パスを提供することもできます。

1switch on i {
2   when 2, 3, 4 {
3       System.debug('when block 2 and 3 and 4');
4   }
5   when 5, 6 {
6       System.debug('when block 5 and 6');
7   }
8   when 7 {
9       System.debug('when block 7');
10   }
11   when else {
12       System.debug('default');
13   }
14}
メソッドの例

可変式で切り替える代わりに、次の例ではメソッドコールの結果で切り替えます。

1switch on someInteger(i) {
2   when 2 {
3       System.debug('when block 2');
4   }
5   when 3 {
6       System.debug('when block 3');
7   }
8   when else {
9       System.debug('default');
10   }
11}

sObject での例

sObject 値で切り替える場合、instanceof チェックとキャストを暗黙的に実行できます。たとえば、if-else ステートメントを使用する次のコードがあるとします。

1if (sobject instanceof Account) {
2    Account a = (Account) sobject;
3    System.debug('account ' + a);
4} else if (sobject instanceof Contact) {
5    Contact c = (Contact) sobject;
6    System.debug('contact ' + a);
7} else {
8    System.debug('default');
9}

このコードを次の switch ステートメントで置き換えて簡略化できます。

1switch on sobject {
2   when Account a {
3       System.debug('account ' + a);
4   }
5   when Contact c {
6       System.debug('contact ' + c);
7   }
8   when null {
9       System.debug('null');
10   }
11   when else {
12       System.debug('default');
13   }
14}

when ブロックごとに 1 つの sObject 型を使用できます。

メモ

列挙での例

列挙 when 値を使用する switch ステートメントでは when else ブロックは必須ではありませんが、使用することをお勧めします。when ブロック句ごとに複数の列挙値を使用できます。

1switch on season {
2   when WINTER {
3       System.debug('boots');
4   }
5   when SPRING, SUMMER {
6       System.debug('sandals');
7   }
8   when else {
9       System.debug('none of the above');
10   }
11}