ルックアップの参照先フィールド

kintoneのためにJavaScriptを初めて2週間ほどの者です。

アプリBに登録されたレコードについて、アプリAからルックアップしたいと思っています。

ラジオボタンかドロップダウンで条件を付け、アプリBにある1つのフィールドに対し、その条件に従って、アプリA内の参照フィールドが変更されるよう設定できるものでしょうか。

以下「参照アプリを動的に変更」を見ていたのですが、参照アプリの変更は必要なく、同一アプリ内で参照フィールドを変えることはできないか、ということになります。

https://developer.cybozu.io/hc/ja/community/posts/360017928983-%E8%87%AA%E4%BD%9C%E3%83%AB%E3%83%83%E3%82%AF%E3%82%A2%E3%83%83%E3%83%97%E3%81%A7%E5%8F%82%E7%85%A7%E3%82%A2%E3%83%97%E3%83%AA%E3%82%92%E5%8B%95%E7%9A%84%E3%81%AB%E5%A4%89%E6%9B%B4

私が作るには難しいとは思うのですが、こういったことが可能かどうかと、参照すべきページが他にありましたら教えてください。

かのこ様

お世話になっております。 cstapの江田と申します。

仰る通り、コードを少し変えて転用できます。

(function(){"use strict";varMyLookUp=(function(fieldSettings){functionMyLookUp(fieldSettings){this.fieldSettings=fieldSettings;}MyLookUp.prototype={createModal:function(){var\_this=this;this.modal=document.createElement('div');this.modalTable=document.createElement('table');this.modalTbody=document.createElement('tbody');this.modal.classList.add('lookUpModal');this.modalTable.classList.add('lookUpModalTable');this.modalTable.innerHTML=('\<thead\>\<tr\>'+this.fieldSettings.viewFields.reduce(function(columns,viewField){returncolumns+'\<th\>'+viewField+'\</th\>';},'')+'\<th\>取得\</th\>'+'\</tr\>\</thead\>');this.modal.addEventListener('click',function(e){if(e.target===\_this.modal){\_this.removeModal();}});this.modalTbody.addEventListener('click',function(e){if(e.target.classList.contains('modalGetButton')){\_this.copyDatas(\_this.records[e.target.getAttribute('data-index')]);}});this.modalTable.appendChild(this.modalTbody);this.modal.appendChild(this.modalTable);document.body.appendChild(this.modal);returnthis;},showModal:function(){var\_this=this;this.modalTbody.innerHTML=(\_this.records.reduce(function(rows,record,index){return(rows+'\<tr\>'+\_this.fieldSettings.viewFields.reduce(function(columns,viewField){returncolumns+'\<td\>'+record[viewField].value+'\</td\>';},'')+'\<td\>\<a class="modalGetButton" data-index="'+index+'"\>取得\</a\>\</td\>'+'\</tr\>')},''));this.modal.classList.add('on');},removeModal:function(){this.modal.classList.remove('on');},createGetButton:function(){var\_this=this;this.getButton=document.createElement('a');this.getButton.innerHTML='取得';this.getButton.classList.add('lookUpButton');this.getButton.addEventListener('click',function(){varquery;\_this.event=kintone.app.record.get();if(\_this.event.record[\_this.fieldSettings.copyField.to].value){query=\_this.event.record[\_this.fieldSettings.copyFieldSelectField].value+'="'+\_this.event.record[\_this.fieldSettings.copyField.to].value+'"';if(\_this.fieldSettings.query)query+=(' and '+\_this.fieldSettings.query);}else{query=\_this.fieldSettings.query;}kintone.api(kintone.api.url('/k/v1/records',true),'GET',{app:\_this.fieldSettings.app,query:query+\_this.fieldSettings.sort}).then(function(response){if(!response.records.length){alert('データがありません。');}elseif(response.records.length===1){\_this.copyDatas(response.records[0]);}else{\_this.records=response.records;\_this.showModal(response.records);}});});returnthis;},createClearButton:function(){var\_this=this;this.clearButton=document.createElement('a');this.clearButton.classList.add('lookUpButton');this.clearButton.innerHTML='クリア';this.clearButton.addEventListener('click',function(){\_this.clearDatas();});returnthis.clearButton;},showButtons:function(){kintone.app.record.getSpaceElement(this.fieldSettings.buttonSpace).appendChild(this.getButton);kintone.app.record.getSpaceElement(this.fieldSettings.buttonSpace).appendChild(this.createClearButton());returnthis;},copyDatas:function(record){var\_this=this;this.event.record[this.fieldSettings.copyField.to].value=record[this.event.record[this.fieldSettings.copyFieldSelectField].value].value;this.fieldSettings.otherCopyFields.forEach(function(otherCopyField){\_this.event.record[otherCopyField.to].value=record[otherCopyField.from].value;});this.event.record[this.fieldSettings.recordIdField].value=record.レコード番号.value;kintone.app.record.set(this.event);this.removeModal();//alert('参照先からデータが取得されました。');},clearDatas:function(record){var\_this=this;this.event.record[this.fieldSettings.copyField.to].value=null;this.fieldSettings.otherCopyFields.forEach(function(otherCopyField){\_this.event.record[otherCopyField.to].value=null;});this.event.record[this.fieldSettings.recordIdField].value=null;kintone.app.record.set(this.event);},disableOtherCopyFields:function(event){this.fieldSettings.otherCopyFields.forEach(function(otherCopyField){event.record[otherCopyField.to].disabled=true;});event.record[this.fieldSettings.recordIdField].disabled=true;//kintone.app.record.setFieldShown(this.fieldSettings.recordIdField, false);returnevent;},createLink:function(event){kintone.app.record.getFieldElement(this.fieldSettings.copyField.to).innerHTML=('\<a href="../'+this.fieldSettings.app+'/show#record='+event.record[this.fieldSettings.recordIdField].value+'" target="\_blank"\>'+event.record[this.fieldSettings.copyField.to].value+'\</a\>');},createLinks:function(event){var\_this=this;event.records.forEach(function(record,index){kintone.app.getFieldElements(\_this.fieldSettings.copyField.to)[index].innerHTML=('\<div\>\<a href="../'+\_this.fieldSettings.app+'/show#record='+record[\_this.fieldSettings.recordIdField].value+'" target="\_blank"\>'+record[\_this.fieldSettings.copyField.to].value+'\</a\>\</div\>');});}}returnMyLookUp;})();varlookUpParams={copyFieldSelectField:'参照',//参照フィールドを決めるフィールドbuttonSpace:'lookUpButton',//ボタン設置用のスペースフィールドrecordIdField:'参照ID',//参照レコードのID保存用のフィールドapp:10116,//関連付けるアプリIDcopyField:{to:'ルックアップ',//自作のルックアップフィールド},otherCopyFields:[//ほかのフィールドのコピー{to:'文字列コピー',from:'文字列'},{to:'日付コピー',from:'日付'},],viewFields:['レコード番号','数値','数値2','文字列','日付'],//コピー元のレコードの選択時に表示するフィールドquery:' 更新日時 \> "2018-07-27T09:00:00+0900" ',//絞り込みsort:' order by レコード番号 asc '//ソート};varlookUP=newMyLookUp(lookUpParams).createGetButton().createModal();kintone.events.on(['app.record.index.show'],function(event){returnlookUP.createLinks(event);});kintone.events.on(['app.record.detail.show'],function(event){returnlookUP.createLink(event);});kintone.events.on(['app.record.create.show','app.record.edit.show'],function(event){lookUP.event=event;returnlookUP.showButtons().disableOtherCopyFields(event);});})();

>江田さま

 

返信が遅くなり失礼いたしました。

ご教示いただいたもので色々と試してみているのですが、ひとつ確認させていただけますでしょうか。。

私の説明が上手くなかったのですが、例えば、

アプリAに「山田さん」のレコードが1つあり、

・会社の住所(郵便番号、都道府県、、、)

・自宅の住所(同上)

の2つが登録されています。

そして、アプリBに「山田さん」を登録する際に、プルダウンで「会社の住所」を選択すると、アプリB の住所欄(←郵便番号、都道府県等のフィールドは1つずつしかない)に、会社の住所がルックアップされる、ということをしたいと思っております。

ご教示いただいたものは、これができるという認識でよろしいでしょうか?

一つずつ調べて進めている状態なので、初心者のやるレベルではないことは重々承知なのですが、ここさえクリアできれば後はベーシックな機能でできそうなため、できれば自力でと思っております。

どうぞよろしくお願いいたします。

 

かのこ様

お世話になっております。

ルックアップフィールドの設定でいうところの「ほかのフィールドのコピー」を可変にするということですね。
上記のコードは「コピー元のフィールド」を可変にするもなのでそのご要望には合致しないかと思います。

上記と同様に、参照記事のコードを書き換えて実装することもできますが、今回の場合でしたらkintoneのルックアップ機能を用いた方が簡単に実装でいるかと思います。

1)アプリBには「郵便番号」、「都道府県」等のフィールドを3ずつ作っておく。
「郵便番号」
「会社_郵便番号」
「自宅_郵便番号」
「都道府県」
「会社_都道府県」
「自宅_都道府県」
・・・

2)「会社_~」、「自宅_~」のフィールドをそれぞれルックアップの「ほかのフィールドのコピー」のコピー先に設定しておく。

3)アプリの設定 > 一覧にて、「会社_~」や「自宅_~」のフィールドを表示しない一覧を作成する。

4)JavaScriptカスタマイズにて、レコード詳細画面、追加画面、編集画面で「会社_~」や「自宅_~」のフィールドを非表示にする。
https://developer.cybozu.io/hc/ja/articles/201941974#step4

5)JavaScriptカスタマイズにて、レコード追加画面、編集画面でsubmitする際に、「郵便番号」には「会社_郵便番号」または「自宅_郵便番号」を、「都道府県」には「会社_都道府県」または「自宅_都道府県」をドロップダウンによって判別してコピーする。
https://developer.cybozu.io/hc/ja/articles/201941984#step2