Avatar

Capturing Specific Field Value From Another App

Ben Clarkson

Hello,

First off, I apologize if the solution to my problem is easy and I am missing something. I am fairly new to working with APIs.

In the app I am working on, I need to iterate through a table and upon each iteration, capture the value of a field in the source app of a lookup field associated with the current table row. I am able to capture the value, but when I attempt to push() that value into an array (which I want to use after the iteration is complete), the order in which the values are added to the array do not mimic the order of the table rows. They seem to change randomly.

Following is a screen capture *.gif of the issue shown in the console. My expected result was that the nums array would show ["4.9"], then ["4.9", "2.5"], then ["4.9", "2.5", "11.7"]. However, in the example below, that array shows as ["4.9"], then ["2.5", "4.9"], then ["4.9", "11.7", "2.5"]. Again, it doesn't always follow this pattern and sometimes the issue doesn't occur until the 6th or 7th event trigger. Any insight would be greatly appreciated.

Here is my code:

(function() {
'use strict';

var testing = [
'app.record.create.change.qty',
'app.record.edit.change.qty'
];


kintone.events.on(testing, function(event) {
var nums = [];
var record = event.record;
var garmentsTable = record.garments.value;

var callback = function(e){
nums.push(e.record.unitPrice.value);
}

var opt_errback = function(event){
var errmsg = 'There was an error when retrieving the data';
if (event.message !== undefined){
errmsg += '\n' + event.message;
}
console.log(errmsg);
}

garmentsTable.forEach((i) => {
var recID = i.value.recID.value;
kintone.api('/k/v1/record', 'GET', {app: 19, id: recID}, callback, opt_errback);
});

console.log(nums)

return event;
});
}());
Follow 3 followers
0

4 comments

Avatar
sanjana 0 votes

An integration engineer often plans, designs and implements the integration process. This can also include creating documentation for the process so that future managers and engineers know how to solve potential issues.

0
Comment actions Permalink
Avatar
Ben Clarkson 0 votes

Hi Sanjana,

 

Thanks for the reply, but I'm a bit confused. Are you saying the only way to solve my problem is to hire an integration engineer?

 

Ben Clarkson

0
Comment actions Permalink
Avatar
Sean Tachibana 0 votes
Hello Ben,
 
I'm getting the record in the API at the time the lookup is done but, because kintone.api is asynchronous processing; this event occurs.
 
If you make it synchronous and make the API processes work in the same order as the table, I think it will work the way you want it to work.
 
Please refer to the following page about the synchronous/asynchronous processing.
 
Using Promises over Synchronous XHR:
 
Also, I was not sure which app ID 19 is the app, but if it's the app from which the lookup originated, then, I feel that you can copy it in the lookup and get it from the event object instead of getting it from the API.
 
Hopefully, this helps.
Sean
0
Comment actions Permalink
Avatar
Ben Clarkson 0 votes

Hello Sean,

 

Thank you for your response and for the tip about capturing the app ID from the lookup field. I will certainly refactor the code as soon as I get the main part of it working.

 

Your solution makes sense. I thought that might have been what my issue was. Ideally, I would just like to just replace the current value of the "Printed Price" field directly with information from the API response, instead of storing it in an array for later use like I had said originally. However, I have had no success with this option. This only seems to be an issue while using a `for` loop or the `forEach()` method for some reason as well.

The main part that confuses me is that when I log the `event.record` I can see that the field value has been updated, but it does not show in the live form. Can you explain why that is?

As always, thank you so much for your help and patience with me.

Here is a screen capture:

Here is my updated code:

(function() {
'use strict';

var testing = [
'app.record.create.change.qty',
'app.record.edit.change.qty'
];

kintone.events.on(testing, function(event) {
var record = event.record;
var garmentsTable = record.garments.value;

garmentsTable.forEach((i) => {
var recID = i.value.recID.value;

var params = {
'app': 19,
'query': '$id = ' + recID,
'fields': ['unitPrice']
}

return kintone.api('/k/v1/records', 'GET', params).then((resp) => {
i.value.printedPrice.value = resp.records[0].unitPrice.value;
console.log(record);
return event;
}, (resp) => {
return event;
});
});

return event;
});
}());
0
Comment actions Permalink