# 添付ファイルを同じアプリ内の添付ファイルフィールドにコピーしたい

**URL:** <https://community.cybozu.dev/t/topic/1664>\
**Category:** kintone 開発相談\
**Created:** [2023 年 6 月 14 日午前 5:31 UTC](https://community.cybozu.dev/t/topic/1664 "2023-06-14T05:31:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Legacy\_Account909](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@Legacy\_Account909](https://community.cybozu.dev/u/Legacy_Account909)\
**Post date:** [2023 年 6 月 14 日午前 5:31 UTC](https://community.cybozu.dev/t/topic/1664/1 "2023-06-14T05:31:54Z")

</div>

有効期限のある認定証を保管する、添付ファイルフィールドの画像を有効期限が切れた段階で、同じレコード内の保存用の過去ファイルフィールドにコピーして、添付ファイルフィールドの画像を消す処理を行いたいのですが、どのように実現したらよいでしょうか？

以下としても、画像は、移動されません。

kintone.events.on(“app.record.edit.show”, function(event) {  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var record=event.record;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;record[‘過去ファイル’].value = record[‘添付ファイル’].value;  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(record[‘添付ファイル’].value.length);  
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; record[‘添付ファイル’].value=[];

&nbsp; &nbsp; &nbsp; &nbsp; return event;  
&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 年 6 月 14 日午後 11:50 UTC](https://community.cybozu.dev/t/topic/1664/2 "2023-06-14T23:50:41Z")

</div>

添付ファイルを他のフィールドにコピーする場合、

①添付ファイルフィールドにあるファイルのファイルキー（ダウンロード用）を使って[ファイルダウンロードAPI](https://cybozu.dev/ja/kintone/docs/rest-api/files/download-file/)を実行  
②ダウンロードしたファイルを[ファイルアップロードAPI](https://cybozu.dev/ja/kintone/docs/rest-api/files/upload-file/)を使ってkintoneの一時保管領域にアップロード  
③レコード詳細画面表示イベントで、②のレスポンスにあるファイルキー（レコード関連付け用）を使って[1件のレコードを更新する](https://cybozu.dev/ja/kintone/docs/rest-api/records/update-record/)等でレコードへ反映（[フィールドの値を書き換える](https://cybozu.dev/ja/kintone/docs/js-api/events/event-object-actions/#record-edit-overwrite-field-values)は添付ファイルフィールド非対応のため）

といった手順を踏まなければなりません。添付ファイルを削除する動作も同様、フィールドの値を書き換えるコードでは動作しないので、REST APIで操作する必要があります。  
[@kintone/rest-api-client](https://cybozu.dev/ja/kintone/sdk/rest-api-client/kintone-javascript-client/)で用意されている[ファイル操作API](https://github.com/kintone/js-sdk/blob/master/packages/rest-api-client/docs/file.md)を使えばフェッチ等を覚える手間が少ないので多少楽に実装できると思います。

---

<div class="post-metadata">

**Author:** ![Legacy\_Account909](https://avatars.discourse-cdn.com/v4/letter/l/b2d939/32.png) [@Legacy\_Account909](https://community.cybozu.dev/u/Legacy_Account909)\
**Post date:** [2023 年 6 月 15 日午前 12:08 UTC](https://community.cybozu.dev/t/topic/1664/3 "2023-06-15T00:08:44Z")

</div>

ご回答ありがとうございました。単純にはできないということですね。  
アドバイスをもとに試行錯誤してみます。

&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 年 10 月 3 日午前 12:32 UTC](https://community.cybozu.dev/t/topic/1664/4 "2023-10-03T00:32:21Z")

</div>

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