# 【kViewer】レコード番号の取得について

**URL:** <https://community.cybozu.dev/t/topic/5616>\
**Category:** kintone 開発相談\
**Created:** [2022 年 1 月 6 日午前 7:07 UTC](https://community.cybozu.dev/t/topic/5616 "2022-01-06T07:07:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Legacy\_Account2937](https://avatars.discourse-cdn.com/v4/letter/l/7ab992/32.png) [@Legacy\_Account2937](https://community.cybozu.dev/u/Legacy_Account2937)\
**Post date:** [2022 年 1 月 6 日午前 7:07 UTC](https://community.cybozu.dev/t/topic/5616/1 "2022-01-06T07:07:59Z")

</div>

kViewerのレコード一覧画面において、一覧に表示されている特定の項目を操作したいです。

#### kv.index.getElementByCode(recordId, code) 関数

を使用したいと思っていますが、レコードIDが必要なため、ページ内に表示されている各レコードのレコードIDをすべて取得したいです。

どのようなロジックで実現するのが良いでしょうか。

ご教示ください。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2229](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Legacy\_Account2229](https://community.cybozu.dev/u/Legacy_Account2229)\
**Post date:** [2022 年 1 月 11 日午前 2:00 UTC](https://community.cybozu.dev/t/topic/5616/2 "2022-01-11T02:00:04Z")

</div>

gonさん

こんにちは。

&nbsp;

kv.events.records.fetchedイベントで、一覧画面のレコード取得API正常終了後の処理を定義できます。

ここで取得されたstate.recordsをループすれば、各レコード情報に対して処理できます。

```
(function() {
'use strict';
kv.events.records.fetched = [function(state) {
state.records.forEach(function(record) {
console.log(record.$id.value);
});
return state;
}];
})();

```

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2937](https://avatars.discourse-cdn.com/v4/letter/l/7ab992/32.png) [@Legacy\_Account2937](https://community.cybozu.dev/u/Legacy_Account2937)\
**Post date:** [2022 年 1 月 11 日午前 7:28 UTC](https://community.cybozu.dev/t/topic/5616/3 "2022-01-11T07:28:12Z")

</div>

koichiさん

ご回答いただきありがとうございます。  
kv.events.records.fetchedでレスポンスの内容を受け取れるということで試してみたところ、

レコード番号を取得することができました。  
大変助かりました。ありがとうございました。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account266](https://avatars.discourse-cdn.com/v4/letter/l/eada6e/32.png) [@Legacy\_Account266](https://community.cybozu.dev/u/Legacy_Account266)\
**Post date:** [2022 年 2 月 19 日午前 10:44 UTC](https://community.cybozu.dev/t/topic/5616/4 "2022-02-19T10:44:07Z")

</div>

gonさん

私も同じところでつまずいています。  
レコード番号取得のサンプルコードを載せていただけますでしょうか。  
宜しくお願い致します。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2229](https://avatars.discourse-cdn.com/v4/letter/l/ce7236/32.png) [@Legacy\_Account2229](https://community.cybozu.dev/u/Legacy_Account2229)\
**Post date:** [2022 年 2 月 25 日午前 6:38 UTC](https://community.cybozu.dev/t/topic/5616/5 "2022-02-25T06:38:07Z")

</div>

foopiiさん

&nbsp;

私が最初の回答いれているのでフォローしますと、上記にあるコードでレコード番号は取得できます。

foreach内の「record.$id.value」がレコード番号です。

&nbsp;

ご不明点ございましたらコメントください。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account266](https://avatars.discourse-cdn.com/v4/letter/l/eada6e/32.png) [@Legacy\_Account266](https://community.cybozu.dev/u/Legacy_Account266)\
**Post date:** [2022 年 2 月 26 日午前 5:08 UTC](https://community.cybozu.dev/t/topic/5616/6 "2022-02-26T05:08:00Z")

</div>

koichi様

ありがとうございます！できました！

ループ処理をどうやって書けばいいか迷っていましたが、  
一応、同じように迷っている誰かがみるかもしれないので、こちらにサンプルとして  
記載しておきます。

&nbsp; &nbsp; &nbsp; for (var i = 0; i \< cnt1.length; i++) {  
&nbsp; &nbsp; &nbsp; &nbsp; for (var j = 0; j \< cnt2.length; j++) {

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var cnt3 = state.records[i][‘S’ + cnt2[j]].value;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (cnt3 \< -1) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;kv.index.getElementByCode(state.records[i].$id.value,‘K’ + cnt2[j]).style.background = ‘#FF88FF’;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}else if (cnt3 \< 0) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;kv.index.getElementByCode(state.records[i].$id.value,‘K’ + cnt2[j]).style.background = ‘#FFDDFF’;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}else if (cnt3 \> 1) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;kv.index.getElementByCode(state.records[i].$id.value,‘K’ + cnt2[j]).style.background = ‘#77FFFF’;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}else if (cnt3 \> 0) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;kv.index.getElementByCode(state.records[i].$id.value,‘K’ + cnt2[j]).style.background = ‘#CCFFFF’;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}  
&nbsp; &nbsp; &nbsp; &nbsp; }  
&nbsp; &nbsp; &nbsp; }

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2937](https://avatars.discourse-cdn.com/v4/letter/l/7ab992/32.png) [@Legacy\_Account2937](https://community.cybozu.dev/u/Legacy_Account2937)\
**Post date:** [2022 年 2 月 26 日午前 8:53 UTC](https://community.cybozu.dev/t/topic/5616/7 "2022-02-26T08:53:10Z")

</div>

foopiiさん

お返事が遅くなりました。ループ処理ですが、私の場合以下のように実現しました。

参考にして頂ければと思います。

```
  var record_number = [];
  var record_count = 0;

  state.records.forEach(function(record) {
     record_number[record_count] = record.$id.value;
     record_count++;
   });

```

---

<div class="post-metadata">

**Author:** ![Legacy\_Account266](https://avatars.discourse-cdn.com/v4/letter/l/eada6e/32.png) [@Legacy\_Account266](https://community.cybozu.dev/u/Legacy_Account266)\
**Post date:** [2022 年 2 月 27 日午前 8:33 UTC](https://community.cybozu.dev/t/topic/5616/8 "2022-02-27T08:33:10Z")

</div>

gon様

ループ処理の実例ありがとうございます。

すっきりしてて、こちらの方がいいですね！

ありがとうございました！

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex015/uploads/cybozu/original/1X/d5c841c3c5a6cb66eed514001c1b45147c9e1f4f.png) [@system](https://community.cybozu.dev/u/system)\
**Post date:** [2024 年 9 月 9 日午前 7:23 UTC](https://community.cybozu.dev/t/topic/5616/9 "2024-09-09T07:23:34Z")

</div>

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