KINTONE初心者です。AIに聞いて下記構文を作成して在庫の増減を自動で入力したいのですが、うまく動きません。どなたかご教授頂けますか?
(function() {
“use strict”;
// 在庫マスターアプリのアプリID
const STOCK_MASTER_APP_ID = 301
// 在庫マスターアプリの在庫数フィールドコード
const STOCK_FIELD_CODE = ‘stock_quantity’;
// 入出庫記録アプリの処理区分フィールドコード
const TRANSACTION_TYPE_FIELD_CODE = ‘transaction_type’;
// 入出庫記録アプリの数量フィールドコード
const QUANTITY_FIELD_CODE = ‘quantity’;
// 入出庫記録アプリのルックアップフィールドコード
const LOOKUP_FIELD_CODE = ‘Product_number’;
// 在庫マスターアプリのレコードIDのフィールドコード(ルックアップで取得するフィールド)
const STOCK_MASTER_ID_FIELD_CODE = ‘id’;
kintone.events.on([
‘app.record.create.submit.success’,
‘app.record.edit.submit.success’
], function(event) {
const record = event.record;
const transactionType = record[TRANSACTION_TYPE_FIELD_CODE].value;
const quantity = record[QUANTITY_FIELD_CODE].value;
const stockMasterId = record[LOOKUP_FIELD_CODE].value[0].id;
if (!stockMasterId) {
console.error("在庫マスターのIDが取得できませんでした。");
return event;
}
// 在庫マスターアプリのレコードを取得
kintone.api.getRecord({
app: STOCK_MASTER_APP_ID,
id: stockMasterId
}).then(function(resp) {
const stockMasterRecord = resp.record;
let currentStock = stockMasterRecord[STOCK_FIELD_CODE].value;
// 入庫処理
if (transactionType === '入庫') {
currentStock += quantity;
}
// 出庫処理
else if (transactionType === '出庫') {
currentStock -= quantity;
}
// 処理区分が入庫でも出庫でもない場合
else {
console.error("処理区分が不正です: " + transactionType);
return event;
}
// 在庫マスターアプリのレコードを更新
const updateData = {};
updateData[STOCK_FIELD_CODE] = currentStock;
kintone.api.updateRecord({
app: STOCK_MASTER_APP_ID,
id: stockMasterId,
record: updateData
}).then(function(resp) {
// 更新成功
console.log("在庫数を更新しました。新しい在庫数: " + currentStock);
return event;
}).catch(function(error) {
// 更新失敗
console.error("在庫数の更新に失敗しました:", error);
return event;
});
}).catch(function(error) {
// レコード取得失敗
console.error("在庫マスターのレコード取得に失敗しました:", error);
return event;
});
});
})();