一覧画面で条件に応じたレコード件数を取得するテクニック

著者名: 落合 雄一 (External link)

目次

はじめに

一覧画面で現在のレコード件数を取得したいことがあるかと思います。
たとえば、以下のようにステータスが「処理中」のレコードの件数を常に表示させておきたい時などに便利です。

デモ環境

デモ環境で実際に動作を確認できます。
https://dev-demo.cybozu.com/k/73/ (External link)

ログイン情報は cybozu developer network デモ環境 で確認してください。

アプリの準備

下記フィールドをフォームに配置します。
今回使用する「ステータス」はラジオボタンフィールドです。
プロセス管理のステータスではありません。

フィールドタイプ フィールド名 フィールドコード 備考
文字列(1行) タスクタイトル タスクタイトル
文字列(複数行) 詳細 詳細
ラジオボタン ステータス status 未処理
処理中
完了

ソース

kintone-rest-api-client の getAllRecord を呼び出しています。
kintone-rest-api-client の使い方は以下を参照ください。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
* Conditional record counts
* Copyright (c) 2014 Cybozu
*
* Licensed under the MIT License
* https://opensource.org/license/mit/
*/
(() =>{
  'use strict';

  // 一覧ページ
  kintone.events.on('app.record.index.show', async (event) => {
    // kintone-rest-api-client を使う準備
    const client = new KintoneRestAPIClient();
    // 絞り込み条件
    const query = kintone.app.getQueryCondition() ? `${kintone.app.getQueryCondition()} and status in ("処理中")` : 'status in ("処理中")';
    // kintone-rest-api-client の getAllRecords を使って全レコードを取得する
    const res = await client.record.getAllRecords({app: kintone.app.getId(), condition: query});
    kintone.app.getHeaderMenuSpaceElement().textContent = `処理中のレコード件数: ${res.length}`;

    return event;
  });
})();
information

この Tips は、2022 年 11 月版 kintone で動作を確認しています。