複数の添付ファイルを別アプリに更新しようとしておりますが、
下記問題点にぶつかりうまくいきません。
何卒ご教示の程よろしくお願いいたします。
①下記【B】のコードでは1つのファイルを更新できますが、複数のファイルを更新する場合、どの部分でループさせれば良いか分かりません。恐れ入りますが、サンプルになるコードをいただけると幸いです。
②var 契約No = event.契約No.value;で得た変数【契約No】を、updateKeyのvalueに入れようとしますが、promise内ではうまくいきません。
どのように処理すればよいでしょうか。
③下記【A】のコード部分で複数の添付ファイルをアップロードする場合、
どのようなボディ構造になりますでしょうか。
※下記は1つの添付ファイルの場合
【A】
var PutPara = {
“app”: 204,
“updateKey”: {
“field”: “契約No”,
“value”: ‘3’
},
“record”: {
“添付ファイル”: {
“value”: [{
“fileKey”: key
}]
}
}
};
何卒よろしくお願いいたします。
【B】
(function () {
“use strict”;
kintone.events.on([‘app.record.edit.submit.success’, ‘app.record.index.edit.submit.success’], function (event) {
var event = event.record;
var 契約No = event.契約No.value;
console.log('契約No = ’ + 契約No);
SyncExecute(event);
});
})();
function SyncExecute(record) {
var fileData = [];
for (key in record.添付ファイル.value[0]) {
fileData.push(record.添付ファイル.value[0][key]);
}
return fileDownload(fileData[0]).then(function (resp) {
fileUpload(fileData[1], fileData[2], resp);
});
function fileDownload(fileKey) {
return new Promise(function (resolve, reject) {
var url = kintone.api.url(‘/k/v1/file’, true) + ‘?fileKey=’ + fileKey;
var xhr = new XMLHttpRequest();
xhr.open(‘GET’, url);
xhr.setRequestHeader(‘X-Requested-With’, ‘XMLHttpRequest’);
xhr.responseType = ‘blob’;
xhr.onload = function () {
if (xhr.status === 200) {
resolve(xhr.response);
} else {
reject(Error(‘ファイルダウンロードエラー :’ + xhr.statusText));
}
};
xhr.onerror = function () {
reject(Error(‘ネットワークエラー’));
};
xhr.send();
});
}
function fileUpload(fileName, contentType, data) {
var blob = new Blob([data], {
type: contentType
});
var formData = new FormData();
formData.append(“__REQUEST_TOKEN__”, kintone.getRequestToken());
formData.append(“file”, blob, fileName);
xmlHttp = new XMLHttpRequest();
xmlHttp.open(“POST”, encodeURI(‘/k/v1/file.json’), false);
xmlHttp.setRequestHeader(‘X-Requested-With’, ‘XMLHttpRequest’);
xmlHttp.responseType = ‘multipart/form-data’;
xmlHttp.send(formData);
var key = JSON.parse(xmlHttp.responseText).fileKey;
var PutPara = {
“app”: 204,
“updateKey”: {
“field”: “契約No”,
“value”: ‘3’
},
“record”: {
“添付ファイル”: {
“value”: [{
“fileKey”: key
}]
}
}
};
kintone.api(kintone.api.url(‘/k/v1/record’, true), ‘PUT’, PutPara, function (resp) {
// success
console.log(‘データの更新に成功しました’);
}, function (error) {
// error
console.log(‘データの更新に失敗しました’);
});
}
}