Salesforce Apexs Object値で切り替えるサンプル
構文
if (sobject instanceof クラス名)
sObject 値で切り替える場合、instanceof チェックとキャストを暗黙的に実行できます。
たとえば、if-else ステートメントを使用する次のコードがあるとします。
使用例
if (sobject instanceof Account) {
Account at = (Account) sobject;
System.debug('account ' + at);
} else if (sobject instanceof Contact) {
Contact ct = (Contact) sobject;
System.debug('contact ' + ct);
} else {
System.debug('test data');
}
if (sobject instanceof Account) {
Account at = (Account) sobject;
System.debug('account ' + at);
} else if (sobject instanceof Contact) {
Contact ct = (Contact) sobject;
System.debug('contact ' + ct);
} else {
System.debug('test data');
}
if (sobject instanceof Account) { Account at = (Account) sobject; System.debug('account ' + at); } else if (sobject instanceof Contact) { Contact ct = (Contact) sobject; System.debug('contact ' + ct); } else { System.debug('test data'); }