# 100件以上のレコードのステータス更新を行いたい

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

<div class="post-metadata">

**Author:** ![Legacy\_Account520](https://avatars.discourse-cdn.com/v4/letter/l/b487fb/32.png) [@Legacy\_Account520](https://community.cybozu.dev/u/Legacy_Account520)\
**Post date:** [2017 年 9 月 20 日午前 10:40 UTC](https://community.cybozu.dev/t/topic/549/1 "2017-09-20T10:40:23Z")

</div>

いつもお世話になっています。

&nbsp;

表題の通り、100件以上（具体的には3000件程度）のレコードのステータス更新を行いたいと思っています。

ステータスのフローは未処理→承認で、実行ユーザーの指定はありません。

kintone Utility Library for javascriptを利用して行おうと思ったのですが、うまくいきません。ステータス更新には対応していないのでしょうか？

対応していない場合、他の実装方法はあるのでしょうか。

以下、サンプルコードです。

```
(function() {
    "use strict";

    // レコード一覧画面
    kintone.events.on('app.record.index.show', function(event) {
        var appId = kintone.app.getId();
        if (event.viewId !== 5359747) {
            return;
        }
                // 増殖バグを防ぐ
        if (document.getElementById('balusButton') !== null) {
            return;
        }
    var statusButtun = document.createElement('button');
        statusButtun.id = 'balusButton';
        statusButtun.innerHTML = '一括承認';
        // ボタンクリック時の処理
        statusButtun.onclick = function() {
            updatestatusValue();
        };

        kintone.app.getHeaderMenuSpaceElement().appendChild(statusButtun);        
        return event;

/////////////////////////////////////////////////////クリック時処理開始
    function updatestatusValue() {
///////////////////////////////////////////////////全レコード取得
        var param = {
        app: event.appId,
        query: '変更対象 in ("変更対象")',
        fields: ['$id','ステータス'],
        totalCount: true,
        isGuest: false
    };
    kintoneUtility.rest.getRecords(param).then(function(resp) {

//////////////////////////////////////////////////// 全レコード更新
                 if (resp.totalCount > 0) {
                    swal({
                        title: 'ステータスを更新します',
                        text: '更新対象レコード ' + resp.totalCount + ' 件',
                        type: 'success',
                        showCancelButton: true,
                        closeOnConfirm: false,
                        showLoaderOnConfirm: true,
                    }, function() {

                        var param = {
                            app: event.appId,
                            query: '変更対象 in ("変更対象")',
                            fields: ['$id','ステータス'],
                            totalCount: false,
                            isGuest: false
                        };
                        kintoneUtility.rest.getAllRecordsByQuery(param).then(function(resp) {
            var rec = event.records;
            var records = [];
            for ( var h in rec ) {
                var obj = {};
                console.log(rec[h]["ステータス"]);
                if ( rec[h]["ステータス"].value === "未処理" ) {
                    obj["id"] = rec[h].$id.value;
                    obj["action"] = "承認";
                    records.push(obj);
                    console.log(obj);
                }
            }
                            var param = {
                                app: event.appId,
                                records: records,
// records: newRecords,
                                isGuest: false
                            };
                            kintoneUtility.rest.putAllRecords(param).then(function(resp) {
//////////////////////////////////////////更新処理完了
                                console.log(JSON.stringify(resp, null, ' '));
        
                                swal({
                                    title: '更新完了!',
                                    text: records.length + ' レコード更新しました',
                                    type: 'success',
                                }, function(resp) {
                                    location.reload();
                                });
                            }).catch(function(error) {
                                // 更新エラー処理
                                sweetAlert("レコード更新エラー！", error.message, "error");
                                console.log(error.message);
                            });
                        }).catch(function(error) {
                            // エラー時の処理
                            sweetAlert("レコード取得エラー！", error.message, "error");
                        });
                    });
                }
                else {
                    swal('更新対象レコードがありません！');
                }

//////////////////////////////////////////取得時処理終了

    }).catch(function(error) {
                sweetAlert("レコード取得エラー！", error.message, "error");
                    console.log(error.message);
});

//////////////////////////////////////////クリック時処理終了
        }
    
    });

})();

```

&nbsp;

以上、よろしくお願いいたします。

&nbsp;

---

<div class="post-metadata">

**Author:** ![Legacy\_Account](https://avatars.discourse-cdn.com/v4/letter/l/ba8739/32.png) [@Legacy\_Account](https://community.cybozu.dev/u/Legacy_Account)\
**Post date:** [2017 年 9 月 21 日午前 12:23 UTC](https://community.cybozu.dev/t/topic/549/2 "2017-09-21T00:23:57Z")

</div>

一括更新は対応しています。おそらくupdateKeyの必須項目が不足しているのではと思います。

サンプルコードもリンク先にのっているので参考にされると良いかと思います。

[https://github.com/kintone/kintoneUtility/blob/master/guides/rest\_doc.md#putAllRecords](https://github.com/kintone/kintoneUtility/blob/master/guides/rest_doc.md#putAllRecords)

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1574](https://avatars.discourse-cdn.com/v4/letter/l/76d3ee/32.png) [@Legacy\_Account1574](https://community.cybozu.dev/u/Legacy_Account1574)\
**Post date:** [2017 年 9 月 21 日午後 1:25 UTC](https://community.cybozu.dev/t/topic/549/3 "2017-09-21T13:25:06Z")

</div>

yukiさんの処理対応は、文章拝見するにステータスフィールドがプロセス管理の設定に伴うもので、/k/v1/records/status.json のAPIによる一括更新を意図されてるのではないでしょうか。 kintoneUtilityは内部的にbulkRequestが利用されていてこれ自体はレコードのステータス一括更新に対応していますが、こちらを見る限り

\> [https://github.com/kintone/kintoneUtility/blob/master/src/js/rest/common/makeBulkParam.js](https://github.com/kintone/kintoneUtility/blob/master/src/js/rest/common/makeBulkParam.js)

kintoneUtilityでは、/k/v1/records/status.json には対応してないということではないでしょうか。エラーメッセージを確認した上で対応進められるとイカと思います（レコード一括更新でレコードのステータス一括更新しようとすると、「ステータスの更新には別のAPIを・・・」みたいなメッセージが出てたような気がします）。
