Update table ⇒ Update corresponding record of another application

Hello Dev teams, can i ask this one! 

When I press the save button, the data is transferred to another app at the edit event.

so, i want to ask

The error appears as follows in the line id (187), please tell me why the error, thank you very much.

Can you tell me what kind of error are you seeing?

Hi  Yuzo Arai,

 When I press the save button for the first time this error occurs.

Thank you so much.

Hi NDC,

From the screenshot,

“Cannot read property ‘value’ of undefined” error

on the 187th line,

 

id : row.value.id.value

 

seems to be caused by not getting the value.

Please check if the description of row.value.id.value is correct.

 

However, as other phenomena could occur, if you run into other problems, please provide us with the source code, if possible.

 

Hopefully, this helps.

Hi Sean Tachibana,

Have a nice day. 

As I answered Mr Yuzo Arai, the data after pressing the save button is still correctly returned when I debug (console) ,. but the returrn line is undefined,
How do I try, and how else to debug?

Would you please do a demo app like me, 1 property for the header and 1 table of 2 properties. just 1 or 2 properties help me to unpack the edit function.

Hi Sean Tachibana,

Hi Yuzo Arai,

I’m sorry for bothering you both. I will stop here …

Hello NDC,

When I debugged the script by doing the following:

console.log(row);
console.log(row.value);
console.log(row.value.id);
console.log(row.value.id.value);
return {
id: row.value.id.value,
record: row.value
};

it gave me an “undefined” error in the third row.
console.log(row.value.id);
This means that the id does not exist. And this is why you are experiencing the error.

I experimented with the script myself,
and when I put a fixed value like the following,
I didn’t see any error:

return {
id: 1,
record: row.value
};

Try to fix your script in a way that specifies a record
that you want to update.

If you need help on debugging,
please use the following page as your help:

Kintone Developer Program - Debugging tips for Kintone JS
https://developer.kintone.io/hc/en-us/articles/115003211768-Debugging-tips-for-Kintone-JS

For you reference, I will leave the script that I used when I was testing below:

(function() {
"use strict";


// 更新時の処理
kintone.events.on("app.record.edit.submit.success", function(event) {


var postApp = 5407; // 別アプリのID
var recId = event.recordId; // テーブルを登録したレコードを更新するためのID
var subTable = 'yaoya_subtable'; //サブテーブルのフィールドコード
var headers = [ //コピーするサブテーブル外のフィールドのフィールドコード
'day_yaoya',
'aaa'
];

var record = event.record;


console.log(event.record[subTable].value.map(function (row) {
headers.forEach(function (header) {
row.value[header] = event.record[header];
});
return row.value;
}))

return kintone.api(kintone.api.url('/k/v1/records', true), 'PUT', {
app: postApp,
records:
event.record[subTable].value.map(function (row) {
headers.forEach(function (header) {
row.value[header] = event.record[header];
});


console.log(row);
console.log(row.value);
console.log(row.value.id);
// console.log(row.value.id.value);


return {
id: 1,
record: row.value
};

})
});
});

})();

Hello Yuzo Arai,

I already know my error is because the two apps don’t have the same id, it is always skewed at birth, because on App A side if there are 2,3,4 rows in table Whereas side id has only 1, then on the B side it will generate 2,3,4 id records, so always deviate, will not be able to correct. It can only be edited when there is 1 row in a table.
as shown below:

Would you please give me any suggestion to solve this place?

Hi NDC,

 

I have checked the process you provided us previously, and the process generates a request by dividing the table rows into records.

Also, since you specified PUT in the previous version, no records should be added.

Therefore, since only the API is switched to POST and have not changed the request generating process, the table rows are likely separated and added by records.

 

It is possible to register a single record containing a multi-row table by executing the following request.

 

var parm = {

"app":4459,

"record":{

"Table": {

"value":[

{

"value":{

"Product NameA":{

"value":"a"

},

"Amount of money A":{

"value":"111"

},

"Within the drop-down period":{

"value":"52Period"

}

}

},

{

"value":{

"Product NameA":{

"value":"bb"

},

"Amount of money A":{

"value":"222"

},

"Within the drop-down period":{

"value":"58Period"

}

}

},

{

"value":{

"Product NameA":{

"value":"cc"

},

"Amount of money A":{

"value":"333"

},

"Within the drop-down period":{

"value":"12Period"

}

}

}

]

},

},

};




kintone.api(

'/k/v1/record',

'POST',

parm,

function(event){console.log(event);},

function(err){console.log(err);}

);

 

You can also update the table for the record specified in “id” with the following request.

 

var parm = {

"app":4459,

"id":1,

"record":{

"Table": {

"value":[

{

"value":{

"Product NameA":{

"value":"a"

},

"Amount of money A":{

"value":"111"

},

"Within the drop-down period":{

"value":"52Period"

}

}

},

{

"value":{

"Product NameA":{

"value":"bb"

},

"Amount of money A":{

"value":"222"

},

"Within the drop-down period":{

"value":"58Period"

}

}

},

{

"value":{

"Product NameA":{

"value":"cc"

},

"Amount of money A":{

"value":"333"

},

"Within the drop-down period":{

"value":"12period"

}

}

}

]

},

},

};




kintone.api(

'/k/v1/record',

'PUT',

parm,

function(event){console.log(event);},

function(err){console.log(err);}

);

Field Types:

https://developer.kintone.io/hc/en-us/articles/212494818-Field-Types

 

Based on the above requests, please review the APIs to be executed and how requests are made.

If not, we would like to know how you would like it to work and the details of the current process you are describing.

 

Hopefully, this helps.

Hi Sean Tachibana,

I have fixed the above error, thanks for your advice.

Would you mind if I could ask more?

**** How do i return the 2nd  table?

Thank you for your help.

Hello NDC,

I’m not quite understanding what you mean by 2nd table.
Can you post the screenshots of your app that clarifies more about what you’re trying to do?

Thank you

Hi Yuzo Arai,

I have chosen another way.

Would you mind if I could ask more?

I want to show the error as the figure I provide when i handle checking :

With my code as below, How do I handle it like the figure I provide?

 

 

(function () {
“use strict”;

// 更新時の処理
kintone.events.on(“app.record.edit.submit”, function (event) {
var record = event.record;
var subTable = record[‘yaoya_subtable’].value;
//var required_id = record[‘id’],
var msgErr_ary = [];
var last_element = subTable[subTable.length - 1];
var first_element = subTable[0];
var elStatus = kintone.app.getFieldElements(‘id’);
var fontColorRed = “#ff0000”;
for (var i = 0; i < subTable.length; i++) {
if (subTable[i].value.id.value != 0) {
subTable[i].value.id.disabled = true;
}
else
if (subTable[i].value.id.value == 0) {
subTable[i].value.id.disabled = false;
}
}

if (first_element.value.id.value != 0) {
for (var i = 0; i < subTable.length; i++) {
if (subTable[i].value.id.value == 0) {
subTable[i].value.id.style.background = fontColorRed;
msgErr_ary.push(subTable[i].value.itemA.value);
}
}
}

if (first_element.value.id.value != 0) {
if (last_element.value.id.value != 0) {
console.log(“Check ok bro!”);
}
}

var msg_str = msgErr_ary.join(’ and ');
event.error = msg_str;
return event;
});
})();

Hi NDC,

I found an article that might fit the description.
Please take a look and see if it accomplishes the desired process.

This sample runs validation checks for values inside tables when saving the record.

Validation checks in tables:
https://developer.kintone.io/hc/en-us/articles/115005616728-Validation-checks-in-tables

Hopefully, this helps.

Hi Sean Tachibana/ Hello Yuzo Arai/

i Did it, tks you/. have a nice day.