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

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

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

下記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 

mionixさん

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

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

江田篤史

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

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

以下、同じコードを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

          }

        }

      });

    });

  });

})();

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;
    });
  });
})();

江田篤史

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

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

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