ルックアップで添付ファイルフィールドをコピー

SpLn様

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

下記のようなコードになります.

(function() {
  "use strict";
  var table = 'テーブル'; //サブテーブルのフィールドコード
  var lookupField = 'ルックアップ';
  var lookupIdField = 'ルックアップID';
  var originAttachmentsField = '添付ファイル';
  var copyAttachmentsField = '添付ファイル';
  kintone.events.on([
    'app.record.detail.show',
    'app.record.create.show',
    'app.record.edit.show',
  ], function(event){
    kintone.app.record.setFieldShown(lookupIdField, false);
    event.record[table].value.forEach(function(row){
      row.value[copyAttachmentsField].disabled = true;
    });
    return event;
  });
  kintone.events.on([
    'app.record.create.submit.success',
    'app.record.edit.submit.success',
  ], function(event){
    var client = new KintoneRestAPIClient();
    return kintone.Promise.all(event.record[table].value.map(function(row){
      if(!row.value[lookupIdField].value) return [];
      return client.record.getRecord({
        app: kintone.app.getLookupTargetAppId(lookupField),
        id: row.value[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(copyFileInfomationsChunks){
      return client.record.updateRecord({
        app: event.appId,
        id: event.recordId,
        record: {
          [table]: {
            value: event.record[table].value.map(function(row, index){
              return {
                value: {
                  ...row.value,
                  [copyAttachmentsField]: {
                    value: copyFileInfomationsChunks[index]
                  }
                }
              }
            })
          }
        }
      });
    });
  });
})();