I can see that the rows are being added correctly when I log the event.record in the browser console. But it is not updating the actual table in the record.
Got an error message? Building a JS customization?
Current Situation
How is your Kintone App configured? What are you trying to do?
I am trying to add data to a table based on related records. The code should get any records from app B that are connected to the current record in app A. It should then pull certain values from the records in app B and enter them into a table in the current record in app A.
Code / Attempts
Share your code and setup
(function() {
'use strict';
kintone.events.on('app.record.edit.show', function(event) {
console.log(event.record);
var school = event.record;
var oldValue = school.Table.value;
var body = { // parameters for the GET API call
app: 362
};
var allSections;
// ** API call to get all records from the Section Information app **
kintone.api(kintone.api.url('/k/v1/records.json', true), 'GET', body, function (success) {
allSections = success.records;
// Continue adjusting the table if the API call returns any records
if (allSections) {
var newValue = [];
// ** Find section records related to current school record **
var schoolSections = [];
for (let i = 0; i < allSections.length; i++) {
if (allSections[i].School_Record_Number.value === school.Record_Number.value) {
schoolSections.push(allSections[i]);
}
}
// Continue adjusting the table if there are any sections related to the current school
if (schoolSections.length > 0) {
for (let j = 0; j < schoolSections.length; j++) {
switch (schoolSections[j].Section_Type.value) {
case 'Maternelle':
break;
case 'Primaire':
// Get total number of students per level from the section record
var noStudentsL1 = schoolSections[j].No_Students_Primaire_Level_1.value;
var noStudentsL2 = schoolSections[j].No_Students_Primaire_Level_2.value;
var noStudentsL3 = schoolSections[j].No_Students_Primaire_Level_3.value;
var noStudentsL4 = schoolSections[j].No_Students_Primaire_Level_4.value;
var noStudentsL5 = schoolSections[j].No_Students_Primaire_Level_5.value;
var noStudentsL6 = schoolSections[j].No_Students_Primaire_Level_6.value;
// Create new rows for each level in the section
if (noStudentsL1 > 0) {
// Find existing row for this section and level, if one exists
var tempSection = schoolSections[j].Section_ID.value;
var tempAssess = '';
var tempBook = '';
for (let k = 0; k < oldValue.length; k++) {
if (oldValue[k].value.Primaire_Level.value === 'Level 1' && oldValue[k].value.Primaire_Level_Section_ID.value === tempSection) {
// Copy the assessment and book information for the new row
tempAssess = oldValue[k].value.Primaire_Level_Assessment_Results.value;
tempBook = oldValue[k].value.Primaire_Level_Curr_Book.value;
}
}
// Create new row
var newRow = createValue_('Level 1', tempSection, noStudentsL1, tempAssess, tempBook);
// Add new row to table
school.Table.value.push(newRow);
}
break;
case 'Secondaire':
break;
default:
break;
}
}
}
}
}, function (error) {
console.log(error);
});
return event;
});
function createValue_(level, sectionID, noStudents, assessResults, currBook) {
return {
'value': {
'Primaire_Level': {
'type': 'DROP_DOWN',
'value': level
},
'Primaire_Level_Section_ID': {
'type': 'SINGLE_LINE_TEXT',
'value': sectionID
},
'Primaire_Level_No_Students': {
'type': 'NUMBER',
'value': noStudents
},
'Primaire_Level_Assessment_Results': {
'type': 'SINGLE_LINE_TEXT',
'value': assessResults
},
'Primaire_Level_Curr_Book': {
'type': 'SINGLE_LINE_TEXT',
'value': currBook
}
}
};
}
Upon reviewing the code you provided, it appears that the process for setting values in the table is implemented within the callback function of the kintone.api function.
When retrieving or setting the values of records within the callback function of the kintone.api function, it is necessary to use the following functions:
○ To retrieve the values of records, use the kintone.app.record.get function
○ To set values in records, use the kintone.app.record.set function
The inability to set values in the table may be due to the absence of the above processes being described.
Modifying the process to retrieve and set record values within the callback function of the kintone.api function may resolve the issue.
Upon reading the articles you attached, I noticed that it mentions this in both articles:
" Notes
kintone.app.record.get and kintone.mobile.app.record.get cannot be called in the kintone.events.on handler. If you need to retrieve record data in the event handler, utilize the event object, as that has the record data inside."
So does this mean I should not be using an event handler? Or that I need to create a record outside of the event handler? I'm not quite sure where to implement these functions based on what the article says.
(function() {
'use strict';
kintone.events.on('app.record.edit.show', function(event) {
console.log(event.record);
var school = event.record;
var oldValue = school.Table.value;
var body = { // Parameters for the GET API call
app: ###
};
var allSections;
// ** API call to get all records from the Section Information app **
kintone.api(kintone.api.url('/k/v1/records.json', true), 'GET', body, function (success) {
allSections = success.records;
var school2 = kintone.app.record.get();
// Continue adjusting the table if the API call returns any records
if (allSections) {
var newValue = [];
// ** Find section records related to the current school record **
var schoolSections = [];
for (let i = 0; i < allSections.length; i++) {
if (allSections[i].School_Record_Number.value === school.Record_Number.value) {
schoolSections.push(allSections[i]);
}
}
// Continue adjusting the table if there are any sections related to the current school
if (schoolSections.length > 0) {
for (let j = 0; j < schoolSections.length; j++) {
switch (schoolSections[j].Section_Type.value) {
case 'Maternelle':
break;
case 'Primaire':
// Get total number of students per level from the section record
var noStudentsL1 = schoolSections[j].No_Students_Primaire_Level_1.value;
var noStudentsL2 = schoolSections[j].No_Students_Primaire_Level_2.value;
var noStudentsL3 = schoolSections[j].No_Students_Primaire_Level_3.value;
var noStudentsL4 = schoolSections[j].No_Students_Primaire_Level_4.value;
var noStudentsL5 = schoolSections[j].No_Students_Primaire_Level_5.value;
var noStudentsL6 = schoolSections[j].No_Students_Primaire_Level_6.value;
// Create new rows for each level in the section
if (noStudentsL1 > 0) {
// Find existing row for this section and level, if one exists
var tempSection = schoolSections[j].Section_ID.value;
var tempAssess = '';
var tempBook = '';
for (let k = 0; k < oldValue.length; k++) {
if (oldValue[k].value.Primaire_Level.value === 'Level 1' && oldValue[k].value.Primaire_Level_Section_ID.value === tempSection) {
// Copy the assessment and book information for the new row
tempAssess = oldValue[k].value.Primaire_Level_Assessment_Results.value;
tempBook = oldValue[k].value.Primaire_Level_Curr_Book.value;
}
}
// Create new row
var newRow = createValue_('Level 1', tempSection, noStudentsL1, tempAssess, tempBook);
// Add new row to table
school2.record.Table.value.push(newRow);
}
break;
case 'Secondaire':
break;
default:
break;
}
}
}
}
kintone.app.record.set(school2);
}, function (error) {
console.log(error);
});
return event;
});
function createValue_(level, sectionID, noStudents, assessResults, currBook) {
return {
'value': {
'Primaire_Level': {
'type': 'DROP_DOWN',
'value': level
},
'Primaire_Level_Section_ID': {
'type': 'SINGLE_LINE_TEXT',
'value': sectionID
},
'Primaire_Level_No_Students': {
'type': 'NUMBER',
'value': noStudents
},
'Primaire_Level_Assessment_Results': {
'type': 'SINGLE_LINE_TEXT',
'value': assessResults
},
'Primaire_Level_Curr_Book': {
'type': 'SINGLE_LINE_TEXT',
'value': currBook
}
}
};
}
})();