# ﾚｺｰﾄﾞ再利用した元のﾚｺｰﾄﾞを操作したい

**URL:** <https://community.cybozu.dev/t/topic/1390>\
**Category:** kintone 開発相談\
**Created:** [2023 年 2 月 14 日午前 4:23 UTC](https://community.cybozu.dev/t/topic/1390 "2023-02-14T04:23:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Legacy\_Account29](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Legacy\_Account29](https://community.cybozu.dev/u/Legacy_Account29)\
**Post date:** [2023 年 2 月 14 日午前 4:23 UTC](https://community.cybozu.dev/t/topic/1390/1 "2023-02-14T04:23:07Z")

</div>

様々なﾌﾟﾛｸﾞﾗﾑを参照してきましたが、なかなか思ったようにいきません。

よろしくお願い致します。

&nbsp;

■実現したいこと

ﾚｺｰﾄﾞ再利用した際に、そのﾚｺｰﾄﾞを最新版と判断できるよう、再利用元のﾚｺｰﾄﾞのｶｳﾝﾀｰ(ﾌｨｰﾙﾄﾞ名：最新判定(文字列))を0に変更したい。

&nbsp;

1.新規作成時に最新判定に1が入力される

2.管理番号の重複は無し

&nbsp;

■現在の状況

再利用ﾚｺｰﾄﾞの保存ﾎﾞﾀﾝを押しても、詳細画面に戻らないが保存はされている

再利用元ﾚｺｰﾄﾞの最新判定は1のまま変化なし

&nbsp;

(() =\> {

&nbsp; ‘use strict’;

&nbsp; kintone.events.on(‘app.record.create.submit.success’, (event) =\> {

&nbsp; &nbsp; const teisyutu = event.record[‘提出回数’][‘value’];

&nbsp; &nbsp; const app = kintone.app.getid();

&nbsp; &nbsp; var kanri = (event.record[“管理番号”][“value”])?parseInt(event.record[“管理番号”][“value”]):0;

&nbsp; &nbsp; kanri = kanri-1;

&nbsp; &nbsp; query = `管理番号 = ${kanri}`;

&nbsp; &nbsp; var params = {app: app,

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; query: query,

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

&nbsp;

&nbsp; &nbsp; if(teisyutu \> 1){ //新規ﾚｺｰﾄﾞ追加時は実行しない

&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;kintone.api(kintone.api.url(‘/k/v1/records’, true), ‘GET’,

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;params).then(function(resp) {

&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; var record = kintone.app.record.get();

&nbsp; &nbsp; &nbsp; &nbsp; resp.record.record[‘最新判定’].value = 0;

&nbsp; &nbsp; &nbsp; &nbsp; kintone.app.record.set(record);

&nbsp; &nbsp; &nbsp; &nbsp;});

&nbsp; };

});

})();

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1177](https://avatars.discourse-cdn.com/v4/letter/l/77aa72/32.png) [@Legacy\_Account1177](https://community.cybozu.dev/u/Legacy_Account1177)\
**Post date:** [2023 年 2 月 15 日午前 4:53 UTC](https://community.cybozu.dev/t/topic/1390/2 "2023-02-15T04:53:45Z")

</div>

詳細画面に戻らないのは

```
    const app = kintone.app.getid();

```

この部分でエラーが起きているためです。アプリIDを求めるにはgetid()ではなくgetId()です。

エラー情報等は[こちら](https://community.cybozu.dev/hc/ja/articles/207613916-%E5%8B%95%E3%81%8B%E3%81%AA%E3%81%84-%E3%81%9D%E3%82%93%E3%81%AA%E6%99%82%E3%81%AF%E3%83%87%E3%83%90%E3%83%83%E3%82%B0%E3%82%92%E3%81%97%E3%81%A6%E3%81%BF%E3%82%88%E3%81%86-%E5%85%A5%E9%96%80%E7%B7%A8?_ga=2.75284241.241293141.1676416166-518509157.1676416166)からコンソールを確認することができるので、エラーが出るか等を参考にされると良いでしょう。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account29](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Legacy\_Account29](https://community.cybozu.dev/u/Legacy_Account29)\
**Post date:** [2023 年 2 月 16 日午前 10:07 UTC](https://community.cybozu.dev/t/topic/1390/3 "2023-02-16T10:07:02Z")

</div>

回答頂きありがとうございます。

ﾚｺｰﾄﾞ詳細画面に戻らない現象は解消されました。

しかし、再利用元のﾚｺｰﾄﾞは変化をさせることができず、ｴﾗｰ表示が遅れて出てきます。

■エラー表示

Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received

■ｺｰﾄﾞ

```
 (() => {

  'use strict';

  // ﾚｺｰﾄﾞ再利用ﾎﾞﾀﾝで編集、保存成功後に適用

  kintone.events.on('app.record.create.submit.success', (event) => {

    const teisyutu = event.record['提出回数']['value'];

    const app = kintone.app.getId();

    var kanri = (event.record["管理番号"]["value"])?parseInt(event.record["管理番号"]["value"]):0;

    kanri = kanri-1;

    var query = '管理番号 = "' + kanri + '"';

    var params = {app: app,

                  query: query,

                 }

    if(teisyutu > 1){ //新規ﾚｺｰﾄﾞ追加時は実行しない

         kintone.api(kintone.api.url('/k/v1/records', true), 'GET',

         params).then(function(resp) {

         

         resp.record.records[0].最新判定.value = 0;

         kintone.app.record.set(record);

       });

  };

  return event;

});

})();

```

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1177](https://avatars.discourse-cdn.com/v4/letter/l/77aa72/32.png) [@Legacy\_Account1177](https://community.cybozu.dev/u/Legacy_Account1177)\
**Post date:** [2023 年 2 月 17 日午前 7:43 UTC](https://community.cybozu.dev/t/topic/1390/4 "2023-02-17T07:43:04Z")

</div>

BTS佐々木 さま

次に発生しているエラーは、処理を続けようとしているものの、先にレコード保存に伴うページ推移が発生している旨のエラーです。この辺りについては、Promiseの理解が必要です。また、そのエラーを解消しても

```
           resp.records[0].最新判定.value = 0;

```

↑取得したレコードに対しての書き換えはできません。実現させるためには、レコードの更新APIが必要です。

```
           kintone.app.record.set(record);

```

↑（処理がPromiseの場合）イベントハンドラ内でkintone.app.record.setは使えないのと、そもそもrecordが宣言されていないのでエラーになります。

と、まだ修正箇所があります。一つ一つ解消と思っていましたが、Promiseの理解には時間が掛かると思うので、取り敢えずは

```
(() => {
    'use strict';

    kintone.events.on([
        'app.record.create.submit.success',
        'mobile.app.record.create.submit.success'
    ], (event) => {
        let appId = event.appId;
        let isReuse = location.href.match(/edit\?/);

        if (!isReuse) return event;

        let recordId = location.href.split(/record=|&reuse/)[1];
        let param = {
            app: appId,
            id: recordId,
            record: {
                '管理番号': {
                    value: 0
                }
            }
        };

        return kintone.api(kintone.api.url('/k/v1/record.json', true), 'PUT', param).then(() => {
            return event;
        }).catch((error) => {
            alert(JSON.stringify(error));
            return event;
        });
    });
})();

```

こちらをお使い下さい。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account29](https://avatars.discourse-cdn.com/v4/letter/l/b3f665/32.png) [@Legacy\_Account29](https://community.cybozu.dev/u/Legacy_Account29)\
**Post date:** [2023 年 2 月 20 日午後 12:44 UTC](https://community.cybozu.dev/t/topic/1390/5 "2023-02-20T12:44:45Z")

</div>

[mls-hashimoto様](https://community.cybozu.dev/hc/ja/profiles/385013133091-mls-hashimoto)

ｺｰﾄﾞを書いていただきありがとうございます。

自分で作ることが出来ず悔しいですが、頂いたｺｰﾄﾞを一つ一つ調べて

ｽｷﾙｱｯﾌﾟできるよう努力します。

&nbsp;

---

<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:** [2023 年 11 月 9 日午前 7:39 UTC](https://community.cybozu.dev/t/topic/1390/6 "2023-11-09T07:39:59Z")

</div>

クローズされました: 2023 年 11 月 9 日午前 7:39 UTC

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