switch文の中に、if文を入れてフィールドの表示・非表示を変えたい

いつもお世話になっております。
switch文の中で、if文を入れて表示・非表示を制御したいのですが、うまくできません…

下記URLを参考にタブを設定して表示変更まではできたのですが、特定のフィールドの文字列のありなしで判定をかけて、タブ内の表示項目を絞り込みたいです。
kintoneのレコード画面をタブ化する(縦長画面防止)
https://aakel-digital.com/LGbkSfGN/BLOG34

■タブ[基本情報] タブ[A単位] タブ[B単位]
↑タブ[A単位]をクリックして、フィールドAに何かの文字列が入っていたら、フィールド[A単位の1]を表示、フィールド[A単位の2]を非表示、フィールド[A単位の3]を非表示
↑タブ[A単位]をクリックして、フィールドBに何かの文字列が入っていたら、フィールド[A単位の2]を非表示、フィールド[A単位の2]を表示、フィールド[A単位の3]を非表示
↑その他の場合はフィールド[A単位の3]のみ表示

というようなことがやりたいです。chatGPTにも聞いてみましたが、うまく行きませんでした。

 

 

(function() {
    'use strict';

  var ButtonAll = document.createElement('button');
  ButtonAll.id = 'eeButton';
  ButtonAll.style.height = '30px';
  ButtonAll.style.width = '120px';
  ButtonAll.innerHTML = ' 全表示 ';
  ButtonAll.style.borderRadius = '10px 10px 0px 0px';
  
  var ButtonA = document.createElement('button');
  ButtonA.id = 'ButtonA';
  ButtonA.style.height = '30px';
  ButtonA.style.width = '150px';
  ButtonA.innerHTML = ' 基本情報 ';
  ButtonA.style.borderRadius = '10px 10px 0px 0px';
  
  var ButtonB = document.createElement('button');
  ButtonB.id = 'ButtonB';
  ButtonB.style.height = '30px';
  ButtonB.style.width = '150px';
  ButtonB.innerHTML = ' A単位 ';
  ButtonB.style.borderRadius = '10px 10px 0px 0px';
  
  var ButtonC = document.createElement('button');
  ButtonC.id = 'ButtonC';
  ButtonC.style.height = '30px';
  ButtonC.style.width = '150px';
  ButtonC.innerHTML = ' B単位 ';
  ButtonC.style.borderRadius = '10px 10px 0px 0px';
  

  var devSpace = document.createElement('dev');
  devSpace.innerHTML = ' ';//タブ開始位置の調整
  
  
  kintone.events.on(['app.record.create.show','app.record.edit.show','app.record.detail.show'], function (e) {
    if (document.getElementById ('button') != null) {
      return;
    }
    kintone.app.record.getSpaceElement('TAB_ID').appendChild(devSpace);
    kintone.app.record.getSpaceElement('TAB_ID').appendChild(ButtonAll);
    kintone.app.record.getSpaceElement('TAB_ID').appendChild(ButtonA);
    kintone.app.record.getSpaceElement('TAB_ID').appendChild(ButtonB);
    kintone.app.record.getSpaceElement('TAB_ID').appendChild(ButtonC);

     tagView("All");//追加
  });

  ButtonAll.onclick = function() {
      tagView("All");
      // グループフィールドの表示・非表示
      kintone.app.record.setGroupFieldOpen("基本情報", true);
      kintone.app.record.setGroupFieldOpen("A単位", true);
      kintone.app.record.setGroupFieldOpen("B単位", true);
      return false;
    }

    ButtonA.onclick = function() {
      tagView("a");
      return false;
    }
    ButtonB.onclick = function() {
      tagView("b");
      return false;
    }
    ButtonC.onclick = function() {
      tagView("c");
      return false;
    }

    function tagView(setInfo){
      ButtonA.style.background = '#c0c0c0';
      ButtonB.style.background = '#c0c0c0';
      ButtonC.style.background = '#c0c0c0';
  
      switch (setInfo) {


          case "a": ButtonA.style.background = '#ff9e3d';
            // グループフィールドの表示・非表示
            kintone.app.record.setFieldShown("基本情報の1", true);
            kintone.app.record.setFieldShown("基本情報の2", false);
            kintone.app.record.setFieldShown("基本情報の3", false);
            break;




          case "b": ButtonB.style.background = '#ff9e3d';

// 【エラー箇所】[フィールドA]に文字列があった時の処理【エラー箇所】
if (event.record.フィールドA.value) {
  kintone.app.record.setFieldShown("A単位の1", true);
  kintone.app.record.setFieldShown("A単位の2", false);
  kintone.app.record.setFieldShown("A単位の3", false);
} else if(event.record.フィールドB.value) {
  kintone.app.record.setFieldShown("A単位の1", false);
  kintone.app.record.setFieldShown("A単位の2", true);
  kintone.app.record.setFieldShown("A単位の3", false);
}
else {
  kintone.app.record.setFieldShown("A単位の1", false);
  kintone.app.record.setFieldShown("A単位の2", false);
  kintone.app.record.setFieldShown("A単位の3", true);
}




// 【エラー箇所】[フィールドA]に文字列があった時の処理(chatGPT回答)【エラー箇所】
var fieldValue = kintone.app.record.getFieldValue('corpNumber');
if (fieldValue !== '') {
    // グループフィールドAを表示する
    kintone.app.record.setFieldShown('A単位の1', true);
    // グループフィールドBを非表示にする
    kintone.app.record.setFieldShown('A単位の2', true);
    // グループフィールドCを非表示にする
    kintone.app.record.setFieldShown('A単位の3', true);
  }


            break;




          case "c": ButtonC.style.background = '#ff9e3d';
            // グループフィールドの表示・非表示
            kintone.app.record.setFieldShown("B単位の1", true);
            kintone.app.record.setFieldShown("B単位の2", false);
            kintone.app.record.setFieldShown("B単位の3", false);
          break;

          default :
            ButtonAll.style.background = '#ff9e3d';
            kintone.app.record.setFieldShown('基本情報',true);
          break;
      }
      return;
  
    };
  })();

ここを直せばいけそうな気がしているのですが、いかがでしょうか…

宜しくお願いします。

// 【エラー箇所】[フィールドA]に文字列があった時の処理【エラー箇所】
if (event.record.フィールドA.value) {
  kintone.app.record.setFieldShown("A単位の1",true);
  kintone.app.record.setFieldShown("A単位の2",false);
  kintone.app.record.setFieldShown("A単位の3",false);
}elseif(event.record.フィールドB.value) {
  kintone.app.record.setFieldShown("A単位の1",false);
  kintone.app.record.setFieldShown("A単位の2",true);
  kintone.app.record.setFieldShown("A単位の3",false);
}
else {
  kintone.app.record.setFieldShown("A単位の1",false);
  kintone.app.record.setFieldShown("A単位の2",false);
  kintone.app.record.setFieldShown("A単位の3",true);
}
// 【エラー箇所】[フィールドA]に文字列があった時の処理(chatGPT回答)【エラー箇所】
var fieldValue = kintone.app.record.getFieldValue('corpNumber');
if (fieldValue !=='') {
   // グループフィールドAを表示する
    kintone.app.record.setFieldShown('A単位の1',true);
   // グループフィールドBを非表示にする
    kintone.app.record.setFieldShown('A単位の2',true);
   // グループフィールドCを非表示にする
    kintone.app.record.setFieldShown('A単位の3',true);
  }

まず

var fieldValue = kintone.app.record.getFieldValue('corpNumber');

getFieldValue()という関数はありません。

次に

// 【エラー箇所】[フィールドA]に文字列があった時の処理【エラー箇所】
if (event.record.フィールドA.value) {

エラーが発生しているのはこの部分ですが、解説含めて以前に私が回答したものと同じです。

早速のご回答ありがとうございます!

教えて頂いた箇所でしたね…すみませんm(_ _)m

if(event.record[‘フィールドA’].value) 
を下記に変更したところ修正できました!!!
********************************************
let recordData = kintone.app.record.get(),
rec = recordData.record;
if(rec[‘フィールドA’].value) 
********************************************

if文は「if(event.record」は固定だと思い込んでおりました
精進します!!

修正後のコードです。

(function() {
  'use strict';

var ButtonAll = document.createElement('button');
ButtonAll.id = 'eeButton';
ButtonAll.style.height = '30px';
ButtonAll.style.width = '120px';
ButtonAll.innerHTML = ' 全表示 ';
ButtonAll.style.borderRadius = '10px 10px 0px 0px';

var ButtonA = document.createElement('button');
ButtonA.id = 'ButtonA';
ButtonA.style.height = '30px';
ButtonA.style.width = '150px';
ButtonA.innerHTML = ' 基本情報 ';
ButtonA.style.borderRadius = '10px 10px 0px 0px';

var ButtonB = document.createElement('button');
ButtonB.id = 'ButtonB';
ButtonB.style.height = '30px';
ButtonB.style.width = '150px';
ButtonB.innerHTML = ' A単位 ';
ButtonB.style.borderRadius = '10px 10px 0px 0px';

var ButtonC = document.createElement('button');
ButtonC.id = 'ButtonC';
ButtonC.style.height = '30px';
ButtonC.style.width = '150px';
ButtonC.innerHTML = ' B単位 ';
ButtonC.style.borderRadius = '10px 10px 0px 0px';


var devSpace = document.createElement('dev');
devSpace.innerHTML = ' ';//タブ開始位置の調整


kintone.events.on(['app.record.create.show','app.record.edit.show','app.record.detail.show'], function (e) {
  if (document.getElementById ('button') != null) {
    return;
  }
  kintone.app.record.getSpaceElement('TAB_ID').appendChild(devSpace);
  kintone.app.record.getSpaceElement('TAB_ID').appendChild(ButtonAll);
  kintone.app.record.getSpaceElement('TAB_ID').appendChild(ButtonA);
  kintone.app.record.getSpaceElement('TAB_ID').appendChild(ButtonB);
  kintone.app.record.getSpaceElement('TAB_ID').appendChild(ButtonC);

   tagView("All");//追加
});

ButtonAll.onclick = function() {
    tagView("All");
    // グループフィールドの表示・非表示
    kintone.app.record.setGroupFieldOpen("基本情報", true);
    kintone.app.record.setGroupFieldOpen("A単位", true);
    kintone.app.record.setGroupFieldOpen("B単位", true);
    return false;
  }

  ButtonA.onclick = function() {
    tagView("a");
    return false;
  }
  ButtonB.onclick = function() {
    tagView("b");
    return false;
  }
  ButtonC.onclick = function() {
    tagView("c");
    return false;
  }

  function tagView(setInfo){
    ButtonA.style.background = '#c0c0c0';
    ButtonB.style.background = '#c0c0c0';
    ButtonC.style.background = '#c0c0c0';

    switch (setInfo) {


        case "a": ButtonA.style.background = '#ff9e3d';
          // グループフィールドの表示・非表示
          kintone.app.record.setFieldShown("基本情報の1", true);
          kintone.app.record.setFieldShown("基本情報の2", false);
          kintone.app.record.setFieldShown("基本情報の3", false);
          break;




case "b": ButtonB.style.background = '#ff9e3d';

// 【修正箇所】[フィールドA]に文字列があった時の処理【修正箇所】
let recordData = kintone.app.record.get(),
rec = recordData.record;
if(rec['フィールドA'].value) {

kintone.app.record.setFieldShown("A単位の1", true);
kintone.app.record.setFieldShown("A単位の2", false);
kintone.app.record.setFieldShown("A単位の3", false);
}
// フィールドAに文字列がない時
 else if(!rec['フィールドA'].value) {
kintone.app.record.setFieldShown("A単位の1", false);
kintone.app.record.setFieldShown("A単位の2", true);
kintone.app.record.setFieldShown("A単位の3", false);
}
break;




        case "c": ButtonC.style.background = '#ff9e3d';
          // グループフィールドの表示・非表示
          kintone.app.record.setFieldShown("B単位の1", true);
          kintone.app.record.setFieldShown("B単位の2", false);
          kintone.app.record.setFieldShown("B単位の3", false);
        break;

        default :
          ButtonAll.style.background = '#ff9e3d';
          kintone.app.record.setFieldShown('基本情報',true);
        break;
    }
    return;

  };
})();

このトピックはベストアンサーに選ばれた返信から 3 日が経過したので自動的にクローズされました。新たに返信することはできません。