Adding rows using Javascript within an API call

Hello @ehoward

Here's an example I made:

(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
			}
		}
	};
}
})();
1 Like