何を実現したいのか?
ドロップダウンの項目の選択によって他フォームの表示、非表示を切り替えたいです。
発生した問題やエラーメッセージ、確認したこと
このコミュニティの中にもhttps://community.cybozu.dev/t/topic/1290/1などで同様の質問はあり、その対処方法が書かれているかと思いますが、その通りに記述しても意図した通りに動作せず、困っています。
下記に実行したコードを載せます。
「affiliatedTeam」はルックアップにより入力される文字列(1行)です。
レコード登録はASTERIAのkintone連携で実行する想定で、その際にルックアップと「affiliatedTeam」は入力される想定です。
問題は「bizCategoryA2Test2」(ドロップダウン)の変更をしたら、それに対応した項目(bizCategoryMeetingLUTest2など)を表示・非表示が切り替わるようにしたいのですが、そのとおりに動かないことです。
現状ではドロップダウンを変更しても非表示のまま切り替わりません。
パターン1(if文を使用)、パターン2(Switch文を使用)で試し、パターン1のときはパターン2をコメントアウト…のように動作確認していますが、いずれの場合でも結果は変わりません。
途中で下記を挿入していますが、そもそもこれが表示されないため、項目変化のイベントを検知できてないんじゃないか?と疑っています。
しかし、だとしてもなぜ検知できてないのかが分かりません。
event.error = 'エラーです!';
JavaScriptおよびkintoneでのJavaScriptでのカスタマイズの経験が浅いため、知恵をお借りしたく思います。
よろしくお願いします。
実行時の画像
※設定画面の画像の右にある項目を表示、非表示を切り替えたい。
例えば、業務カテゴリで「展示会、WEBセミナー、研修」を選択した場合、業務カテゴリと一番右の文字列(1行)のみを表示させたい。
しかし、選択しても文字列(1行)は出て来ない
実行したコード
(function() {
'use strict';
// レコードの追加、編集、詳細画面で適用する
const events = [
'app.record.detail.show',
'app.record.create.show',
'app.record.edit.show',
'app.record.create.change.affiliatedTeam',
'app.record.edit.change.affiliatedTeam'
];
kintone.events.on(events, (event) => {
const record = event.record;
// 所属チームの値を取得する
const affiliatedTeam = record.affiliatedTeam.value;
// 所属チームがAチームの場合、Aチーム向けのカテゴリを表示
if (affiliatedTeam === "Aチーム") {
kintone.app.record.setFieldShown('bizCategoryA2Test2', true);
} else {
kintone.app.record.setFieldShown('bizCategoryA2Test2', false);
}
if (affiliatedTeam === "Bチーム") {
kintone.app.record.setFieldShown('bizCategoryB2Test2', true);
} else {
kintone.app.record.setFieldShown('bizCategoryB2Test2', false);
}
kintone.app.record.setFieldShown('bizCategoryMeetingLUTest2', false);
kintone.app.record.setFieldShown('bizCategoryMeetingStrTest2', false);
kintone.app.record.setFieldShown('bizCategoryProjectLUTest2', false);
kintone.app.record.setFieldShown('bizCategoryProjectStrTest2', false);
kintone.app.record.setFieldShown('bizCategorySemminerTest2', false);
kintone.app.record.setFieldShown('resaleProviderTest2', false);
return event;
});
const listChangeEvents = [
'app.record.create.change.bizCategoryA2Test2',
'app.record.edit.change.bizCategoryA2Test2'
];
kintone.events.on(listChangeEvents, (event) => {
event.error = 'エラーです!';
const record = event.record;
const bizCategoryA2Test2 = record.bizCategoryA2Test2.value;
// パターン1、if文使用
if (record['bizCategoryA2Test2'].value === "打合せ・相談(案件以外)") {
kintone.app.record.setFieldShown('bizCategoryMeetingLUTest2', true);
kintone.app.record.setFieldShown('bizCategoryMeetingStrTest2', true);
} else {
kintone.app.record.setFieldShown('bizCategoryMeetingLUTest2', false);
kintone.app.record.setFieldShown('bizCategoryMeetingStrTest2', false);
}
if (record['bizCategoryA2Test2'].value === "業務改善、プロジェクト") {
kintone.app.record.setFieldShown('bizCategoryProjectLUTest2', true);
kintone.app.record.setFieldShown('bizCategoryProjectStrTest2', true);
} else {
kintone.app.record.setFieldShown('bizCategoryProjectLUTest2', false);
kintone.app.record.setFieldShown('bizCategoryProjectStrTest2', false);
}
if (record['bizCategoryA2Test2'].value === "展示会、WEBセミナー、研修") {
kintone.app.record.setFieldShown('bizCategorySemminerTest2', true);
} else {
kintone.app.record.setFieldShown('bizCategorySemminerTest2', false);
}
// パターン2、switch文使用
switch (bizCategoryA2Test2) {
case '打合せ・相談(案件以外)':
kintone.app.record.setFieldShown('bizCategoryMeetingLUTest2', true);
kintone.app.record.setFieldShown('bizCategoryMeetingStrTest2', true);
kintone.app.record.setFieldShown('bizCategoryProjectLUTest2', false);
kintone.app.record.setFieldShown('bizCategoryProjectStrTest2', false);
kintone.app.record.setFieldShown('bizCategorySemminerTest2', false);
break;
case '業務改善、プロジェクト':
kintone.app.record.setFieldShown('bizCategoryMeetingLUTest2', false);
kintone.app.record.setFieldShown('bizCategoryMeetingStrTest2', false);
kintone.app.record.setFieldShown('bizCategoryProjectLUTest2', true);
kintone.app.record.setFieldShown('bizCategoryProjectStrTest2', true);
kintone.app.record.setFieldShown('bizCategorySemminerTest2', false);
break;
case '展示会、WEBセミナー、研修':
kintone.app.record.setFieldShown('bizCategoryMeetingLUTest2', false);
kintone.app.record.setFieldShown('bizCategoryMeetingStrTest2', false);
kintone.app.record.setFieldShown('bizCategoryProjectLUTest2', false);
kintone.app.record.setFieldShown('bizCategoryProjectStrTest2', false);
kintone.app.record.setFieldShown('bizCategorySemminerTest2', true);
break;
default:
kintone.app.record.setFieldShown('bizCategoryMeetingLUTest2', false);
kintone.app.record.setFieldShown('bizCategoryMeetingStrTest2', false);
kintone.app.record.setFieldShown('bizCategoryProjectLUTest2', false);
kintone.app.record.setFieldShown('bizCategoryProjectStrTest2', false);
kintone.app.record.setFieldShown('bizCategorySemminerTest2', false);
break;
}
return event;
});
})();
