# サブテーブルのレコードを取得→カスタムビュー表示

**URL:** <https://community.cybozu.dev/t/topic/614>\
**Category:** kintone 開発相談\
**Created:** [2017 年 11 月 10 日午前 7:34 UTC](https://community.cybozu.dev/t/topic/614 "2017-11-10T07:34:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Legacy\_Account1849](https://avatars.discourse-cdn.com/v4/letter/l/8491ac/32.png) [@Legacy\_Account1849](https://community.cybozu.dev/u/Legacy_Account1849)\
**Post date:** [2017 年 11 月 10 日午前 7:34 UTC](https://community.cybozu.dev/t/topic/614/1 "2017-11-10T07:34:23Z")

</div>

&nbsp;お世話になっております。

&nbsp;

サブテーブル内のデータを、カスタムビューに表示しようとしています。

カスタムビューの中では、指定した日付の販売実績レコードを店舗別に

表示しているのですが、その中に、詳細画面の中で設定した

「ロス商品テーブル」の内容を表示したいと考えています。

&nbsp;

下記のように記載したいのですが、レコードそのものは取得できるものの、

サブテーブルの中だけを取得することが出来ていません。

&nbsp;

そもそも、サブテーブルのデータを一括で取得し、変数に格納するのは

難しいのでしょうか？

&nbsp;

お分かりの方がいれば、ご教示いただきたく、よろしくお願いいたします。

(function() {  
&nbsp; &nbsp; kintone.events.on(“app.record.index.show”, function(event) {  
&nbsp; &nbsp;&nbsp;var strQuery\_meisai = “日付 = "” + mm\_targetDate.format(“YYYY-MM-DD”) + “"”  
&nbsp; &nbsp;&nbsp;var strFileld = “‘店舗名’,‘税抜売上額’,‘全体考察’,‘T\_Loss’” //T\_Loss=ロス商品を管理するサブテーブル  
&nbsp; &nbsp;&nbsp;var obj = {};

&nbsp; &nbsp;&nbsp;// 当日データ取得→表示  
&nbsp; &nbsp;&nbsp;fetchRecords(fetchRecords(event.appId, strQuery\_meisai , strFileld)).then(function(records) {  
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;obj.m\_rs = convertToRows(records);  
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;console.log('m\_rs: ', JSON.stringify(obj.m\_rs)); //ここではサブテーブルの内容が取得できているのを確認済み

&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;var hansoku = records.T\_Hansoku;  
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;var hansoku2 = obj.m\_rs.T\_Hansoku;

&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;console.log('T\_販促(recordsから取得) : ', JSON.stringify(hansoku)); //戻り値 undifined  
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;console.log('T\_販促(obj.m\_rsから取得) : ', JSON.stringify(hansoku2)); //戻り値 undifined

&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;var m\_result = alasql("SELECT a.\* FROM ? AS a ", [obj.m\_rs]);

&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;for (var i = 0; i \< m\_result.length; i++) {  
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;var m\_record = m\_result[i];  
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;var subTable1 = {};  
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;subTable1 = m\_record.T\_Hansoku;  
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;console.log(“T\_販促(m\_record.T\_Hansokuから取得) ：” + JSON.stringify(subTable1)); //戻り値 []

&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;var subTable2 = {};  
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;subTable2 = m\_record.T\_Hansoku.value;  
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;console.log(“T\_販促(m\_record.T\_Hansoku.valueから取得) ：” + JSON.stringify(subTable2)); //戻り値 undifined  
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;}  
&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;};  
&nbsp; &nbsp;&nbsp;｝  
}

&nbsp;

&nbsp;

function fetchRecords(appId, opt\_query, opt\_fields, opt\_offset, opt\_limit, opt\_records) {  
var query = opt\_query || ‘’;  
var offset = opt\_offset || 0;  
var limit = opt\_limit || 500;  
var allRecords = opt\_records || [];  
var params = {app: appId, query: query + ’ limit ’ + limit + ’ offset ’ + offset };  
if (opt\_fields) params.fields = opt\_fields;  
[//console.log](https://console.log)('opt\_queryの値 ', JSON.stringify(opt\_query));  
[//console.log](https://console.log)(‘queryの値 ‘, JSON.stringify(params));  
return kintone.api(kintone.api.url(’/k/v1/records’, true), ‘GET’, params).then(function(resp) {  
allRecords = allRecords.concat(resp.records);  
if (resp.records.length === limit) {  
return fetchRecords(appId, query, opt\_fields, offset + limit, limit, allRecords);  
}  
return allRecords;  
});  
}

&nbsp;

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1517](https://avatars.discourse-cdn.com/v4/letter/l/4bbf92/32.png) [@Legacy\_Account1517](https://community.cybozu.dev/u/Legacy_Account1517)\
**Post date:** [2017 年 11 月 13 日午後 11:31 UTC](https://community.cybozu.dev/t/topic/614/2 "2017-11-13T23:31:27Z")

</div>

遠藤 さん

records、obj.m\_rs とも配列ですので、サブテーブルを参照する場合は、配列の添え字指定が必要です。

var hansoku = records.T\_Hansoku;  
var hansoku2 = obj.m\_rs.T\_Hansoku;

&nbsp;

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1849](https://avatars.discourse-cdn.com/v4/letter/l/8491ac/32.png) [@Legacy\_Account1849](https://community.cybozu.dev/u/Legacy_Account1849)\
**Post date:** [2017 年 11 月 17 日午前 12:59 UTC](https://community.cybozu.dev/t/topic/614/3 "2017-11-17T00:59:53Z")

</div>

rex0220様

お恥ずかしい限りです。ありがとうございました。
