About GET processing in the for statement

I'm implementing the code below, but it quits all at once without looping  

(function() {

“use strict”;

// 在庫アプリ操作
var syohinDao = {};
syohinDao.APP_ID = 373;

// 新規レコードの保存イベント
kintone.events.on(‘app.record.create.submit’, function(event) {

var tabRec = event.record.入出庫.value;
for (var i = 0; i < tabRec.length; i++) {

console.log(tabRec.length);

var record = event.record;
var params = {
“app”: syohinDao.APP_ID,
“query”: ‘部品コード ="’ + tabRec[i].value[‘部品コード’][‘value’] + ‘"’,
“fields”: [’$id’, ‘$revision’, ‘在庫数’]
};

console.log(params);

return kintone.api(’/k/v1/records’, ‘GET’, params).then(function(resp) {
var syohinRecord = resp[‘records’][0];

if (syohinRecord.length !== 0) {
var updQuantity = 0;
// 入庫、等の場合
if (tabRec[i].value[‘入庫数’][‘value’]) {
updQuantity = Number(syohinRecord[‘在庫数’][‘value’]) + Number(tabRec[i].value[‘入庫数’][‘value’]);
}
// 出庫、等の場合
if (tabRec[i].value[‘出庫数’][‘value’]) {
updQuantity = Number(syohinRecord[‘在庫数’][‘value’]) - Number(tabRec[i].value[‘出庫数’][‘value’]);
}
if (updQuantity < 0) {
// 更新後の在庫数がマイナスになる場合はエラーにして中断
alert(“在庫が足りません。”);
tabRec[i].value[‘出庫数’][‘error’] = “在庫が足りません。”;
event[‘error’] = “在庫が足りません。”;
return event;
}

// 更新パラメータ
var params2 = {
// 商品アプリ番号
“app”: syohinDao.APP_ID,
// レコード番号
“id”: syohinRecord[’$id’][‘value’],
// レビジョン
“revision”: syohinRecord[’$revision’][‘value’],
// 登録データ
“record”: {
“在庫数”: {
“value”: updQuantity
}
}
};
// 商品レコードを更新
return kintone.api(kintone.api.url(’/k/v1/record’, true), ‘PUT’, params2).then(function(resp2) {
alert(“商品レコードを更新しました。”);
}, function(resp2) {
alert(“商品レコードを取得できません。”);
});
}
}, function(resp) {
alert(“商品レコードを取得できません。”);
event[‘error’] = “商品レコードを取得できません。”;
return event;
});
}
});
});

&nbsp;

Hi TS44

Could you explain what you are trying to accomplish with the code?

Not all people in this community can read Japanese, so it’s not that easy to read from the code.

I’m very sorry

I want to reflect the issue value of the part entered in the A app (warehousing / delivery) in the B app based on the part name,
but the “for” command works only once.

(function() {

“use strict”;

// Inventory app operation
var syohinDao = {};
syohinDao.APP_ID = 373;

// Save new record event
kintone.events.on(‘app.record.create.submit’, function(event) {

var tabRec = event.record.LoadingUnloading.value;
for (var i = 0; i < tabRec.length; i++) {

console.log(tabRec.length);

var record = event.record;
var params = {
“app”: syohinDao.APP_ID,
“query”: ‘PartCode ="’ + tabRec[i].value[‘PartCode’][‘value’] + ‘"’,
“fields”: [’$id’, ‘$revision’, ‘StockQuantity’]
};

console.log(params);

return kintone.api(’/k/v1/records’, ‘GET’, params).then(function(resp) {
var syohinRecord = resp[‘records’][0];

if (syohinRecord.length !== 0) {
var updQuantity = 0;
// In case of warehousing
if (tabRec[i].value[‘NumberWarehousing’][‘value’]) {
updQuantity = Number(syohinRecord[‘StockQuantity’][‘value’]) + Number(tabRec[i].value[‘NumberWarehousing’][‘value’]);
}
// In case of delivery
if (tabRec[i].value[‘NumberIssues’][‘value’]) {
updQuantity = Number(syohinRecord[‘StockQuantity’][‘value’]) - Number(tabRec[i].value[‘StockQuantity’][‘value’]);
}
if (updQuantity < 0) {
// If the updated inventory becomes negative, make an error and interrupt
alert(“Not enough stock”);
tabRec[i].value[‘NumberIssues’][‘error’] = “Not enough stock”;
event[‘error’] = “Not enough stock”;
return event;
}

// Update parameters
var params2 = {
// Product app number
“app”: syohinDao.APP_ID,
// Record number
“id”: syohinRecord[’$id’][‘value’],
// Revision
“revision”: syohinRecord[’$revision’][‘value’],
// Registration data
“record”: {
“StockQuantity”: {
“value”: updQuantity
}
}
};
// Update product record
return kintone.api(kintone.api.url(’/k/v1/record’, true), ‘PUT’, params2).then(function(resp2) {
alert(“The product record has been updated”);
}, function(resp2) {
alert(“Unable to get product record”);
});
}
}, function(resp) {
alert(“Unable to get product record”);
event[‘error’] = “Unable to get product record”;
return event;
});
}
});
});

Hi TS44,

It’s hard to explain the synchronization process because it’s a very generic part of the process.
Make a long story short; it is a code that is only executed once because of a code description problem.

I don’t know if I can make myself clear, but here’s the situation.

I think you are using Promise to synchronize GET and PUT processing in a for statement.
The return operation performs when the process finishes, resulting in a situation where the process executes only once.

※The kintone.api method will return the kintone Promise object if the callback omits.

There are several ways to avoid the above events, and how to fix them is a matter of programming preference, but in any case, I think the process itself needs to be significantly modified.

The least modified one would be to stop using Promise and write it with async/await.

Here is a sample:

kintone.events.on('app.record.create.submit', async function(event) {

var tabRec = event.record.LoadingUnloading.value;
for (var i = 0; i < tabRec.length; i++) {

var params = {
//Parameter Creation
};

var getrecord = await kintone.api(kintone.api.url('/k/v1/records', true), 'GET', params);

var params2 = {
// Create parameters using getrecord
}

var putrecord = await kintone.api(kintone.api.url('/k/v1/record', true), 'PUT', params2);
}

});

In the above sample, I did not use Promise, but the event executes in the order in which I write it, so the event will end after the for statement completes.

As for async/await, you would want to Google that, but I found a post explaining it, so please look if you would like.

How and when to use ‘async’ and ‘await’:
https://stackoverflow.com/questions/14455293/how-and-when-to-use-async-and-await

Hopefully, this helps.