# 添付ファイルのルックアップについて

**URL:** <https://community.cybozu.dev/t/topic/7377>\
**Category:** kintone 開発相談\
**Created:** [2021 年 2 月 10 日午前 5:33 UTC](https://community.cybozu.dev/t/topic/7377 "2021-02-10T05:33:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Legacy\_Account2502](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@Legacy\_Account2502](https://community.cybozu.dev/u/Legacy_Account2502)\
**Post date:** [2021 年 2 月 10 日午前 5:33 UTC](https://community.cybozu.dev/t/topic/7377/1 "2021-02-10T05:33:57Z")

</div>

お世話になっております。

ルックアップで添付ファイルをコピーしたいと考えているのですが、

下記URLで一件のみ対応できましたが、3つのルックアップに対応するには

どのようにしたらよいのでしょうか。

また、少しコード変えてみたのですが、エラーになってしまい詰まっております。

ご教示頂けると幸いです。

[https://developer.cybozu.io/hc/ja/community/posts/900001006786-%E3%83%AB%E3%83%83%E3%82%AF%E3%82%A2%E3%83%83%E3%83%97%E3%81%A7%E6%B7%BB%E4%BB%98%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%83%95%E3%82%A3%E3%83%BC%E3%83%AB%E3%83%89%E3%82%92%E3%82%B3%E3%83%94%E3%83%BC](https://developer.cybozu.io/hc/ja/community/posts/900001006786-%E3%83%AB%E3%83%83%E3%82%AF%E3%82%A2%E3%83%83%E3%83%97%E3%81%A7%E6%B7%BB%E4%BB%98%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%83%95%E3%82%A3%E3%83%BC%E3%83%AB%E3%83%89%E3%82%92%E3%82%B3%E3%83%94%E3%83%BC)&nbsp;

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1740](https://avatars.discourse-cdn.com/v4/letter/l/ed8c4c/32.png) [@Legacy\_Account1740](https://community.cybozu.dev/u/Legacy_Account1740)\
**Post date:** [2021 年 2 月 16 日午前 7:23 UTC](https://community.cybozu.dev/t/topic/7377/2 "2021-02-16T07:23:08Z")

</div>

mionixさん

フィールドコードの変数名を変えるなどして，同じようなコードを3回書けば実装できるかと思います．  
あるいは，スマートに書くのであれば，フィールドコードを配列にして繰り返し処理させれば実装できます．

もしよろしければ，書き換えたコードを見せていただけますか？

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2502](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@Legacy\_Account2502](https://community.cybozu.dev/u/Legacy_Account2502)\
**Post date:** [2021 年 2 月 17 日午後 11:39 UTC](https://community.cybozu.dev/t/topic/7377/3 "2021-02-17T23:39:28Z")

</div>

[江田篤史](https://community.cybozu.dev/hc/ja/profiles/5826575706-%E6%B1%9F%E7%94%B0%E7%AF%A4%E5%8F%B2) 様

遅くなってしまい申し訳ありません。

お返事いただきありがとうございます。

以下、同じコードを2回書いたものになります。

このままではエラーになってしまうのですが、どのようにコードを追加すればよいのでしょうか。

どうしても実現させたい内容ですので大変お手数ですが、何卒ご教示お願いいたします。

```
(function ()

{

  "use strict";

  var lookupField = 'ルックアップ';

  var lookupIdField = 'ID';

  var copyAttachmentsField = '添付ファイル';

  var lookupField1 = 'ルックアップ1';

  var lookupIdField1 = 'ID_1';

  var copyAttachmentsField1 = '添付ファイル1';

  var lookupField2 = 'ルックアップ2';

  var lookupIdField2 = 'ID_2';

  var copyAttachmentsField2 = '添付ファイル2';

  var originAttachmentsField = '電子印';

  kintone.events.on([

    'app.record.create.submit.success',

    'app.record.edit.submit.success',

  ], function (event)

  {

    var client = new KintoneRestAPIClient;

    return client.record.getRecord({

      app: kintone.app.getLookupTargetAppId(lookupField),

      id: event.record[lookupIdField].value,

    }).then(function (originRecord)

    {

      return kintone.Promise.all(originRecord.record[originAttachmentsField].value.map(function (originFileInfomation)

      {

        return client.file.downloadFile({

          fileKey: originFileInfomation.fileKey

        }).then(function (fileData)

        {

          return {

            file: {

              name: originFileInfomation.name,

              data: new Blob([fileData], { type: originFileInfomation.contentType })

            }

          };

        });

      }));

    }).then(function (files)

    {

      return kintone.Promise.all(files.map(function (files)

      {

        return client.file.uploadFile(files);

      }));

    }).then(function (copyFileInfomations)

    {

      return client.record.updateRecord({

        app: event.appId,

        id: event.recordId,

        record: {

          [copyAttachmentsField]: {

            value: copyFileInfomations

          }

        }

      });

    });

  });

  kintone.events.on([

    'app.record.create.submit.success',

    'app.record.edit.submit.success',

  ], function (event)

  {

    var client = new KintoneRestAPIClient;

    return client.record.getRecord({

      app: kintone.app.getLookupTargetAppId(lookupField1),

      id: event.record[lookupIdField1].value,

    }).then(function (originRecord)

    {

      return kintone.Promise.all(originRecord.record[originAttachmentsField].value.map(function (originFileInfomation)

      {

        return client.file.downloadFile({

          fileKey: originFileInfomation.fileKey

        }).then(function (fileData)

        {

          return {

            file: {

              name: originFileInfomation.name,

              data: new Blob([fileData], { type: originFileInfomation.contentType })

            }

          };

        });

      }));

    }).then(function (files)

    {

      return kintone.Promise.all(files.map(function (files)

      {

        return client.file.uploadFile(files);

      }));

    }).then(function (copyFileInfomations)

    {

      return client.record.updateRecord({

        app: event.appId,

        id: event.recordId,

        record: {

          [copyAttachmentsField1]: {

            value: copyFileInfomations

          }

        }

      });

    });

  });

})();

```

---

<div class="post-metadata">

**Author:** ![Legacy\_Account1740](https://avatars.discourse-cdn.com/v4/letter/l/ed8c4c/32.png) [@Legacy\_Account1740](https://community.cybozu.dev/u/Legacy_Account1740)\
**Post date:** [2021 年 2 月 18 日午前 12:29 UTC](https://community.cybozu.dev/t/topic/7377/4 "2021-02-18T00:29:12Z")

</div>

mionixさん

申し訳ございません． これは私の記事のコードが良くないです．

各処理で最後にeventオブジェクトをreturnするようにしてください．

```
(function () {
  "use strict";
  var lookupField = 'ルックアップ';
  var lookupIdField = 'ID';
  var copyAttachmentsField = '添付ファイル';
  var lookupField1 = 'ルックアップ1';
  var lookupIdField1 = 'ID_1';
  var copyAttachmentsField1 = '添付ファイル1';
  var lookupField2 = 'ルックアップ2';
  var lookupIdField2 = 'ID_2';
  var copyAttachmentsField2 = '添付ファイル2';
  var originAttachmentsField = '電子印';
  kintone.events.on([
    'app.record.create.submit.success',
    'app.record.edit.submit.success',
  ], function (event) {
    var client = new KintoneRestAPIClient;
    return client.record.getRecord({
      app: kintone.app.getLookupTargetAppId(lookupField),
      id: event.record[lookupIdField].value,
    }).then(function (originRecord) {
      return kintone.Promise.all(originRecord.record[originAttachmentsField].value.map(function (originFileInfomation) {
        return client.file.downloadFile({
          fileKey: originFileInfomation.fileKey
        }).then(function (fileData) {
          return {
            file: {
              name: originFileInfomation.name,
              data: new Blob([fileData], { type: originFileInfomation.contentType })
            }
          };
        });
      }));
    }).then(function (files) {
      return kintone.Promise.all(files.map(function (files) {
        return client.file.uploadFile(files);
      }));
    }).then(function (copyFileInfomations) {
      return client.record.updateRecord({
        app: event.appId,
        id: event.recordId,
        record: {
          [copyAttachmentsField]: {
            value: copyFileInfomations
          }
        }
      });
    }).then(function (){ //追加
      return event;
    });
  });
  kintone.events.on([
    'app.record.create.submit.success',
    'app.record.edit.submit.success',
  ], function (event) {
    var client = new KintoneRestAPIClient;
    return client.record.getRecord({
      app: kintone.app.getLookupTargetAppId(lookupField1),
      id: event.record[lookupIdField1].value,
    }).then(function (originRecord) {
      return kintone.Promise.all(originRecord.record[originAttachmentsField].value.map(function (originFileInfomation) {
        return client.file.downloadFile({
          fileKey: originFileInfomation.fileKey
        }).then(function (fileData) {
          return {
            file: {
              name: originFileInfomation.name,
              data: new Blob([fileData], { type: originFileInfomation.contentType })
            }
          };
        });
      }));
    }).then(function (files) {
      return kintone.Promise.all(files.map(function (files) {
        return client.file.uploadFile(files);
      }));
    }).then(function (copyFileInfomations) {
      return client.record.updateRecord({
        app: event.appId,
        id: event.recordId,
        record: {
          [copyAttachmentsField1]: {
            value: copyFileInfomations
          }
        }
      });
    }).then(function (){ //追加
      return event;
    });
  });
})();

```

---

<div class="post-metadata">

**Author:** ![Legacy\_Account2502](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@Legacy\_Account2502](https://community.cybozu.dev/u/Legacy_Account2502)\
**Post date:** [2021 年 2 月 18 日午前 12:38 UTC](https://community.cybozu.dev/t/topic/7377/5 "2021-02-18T00:38:55Z")

</div>

[江田篤史](https://community.cybozu.dev/hc/ja/profiles/5826575706-%E6%B1%9F%E7%94%B0%E7%AF%A4%E5%8F%B2) 様

ご返答ありがとうございます。

return することで無事実装することができました。

誠にありがとうございました。

&nbsp;
