What is the best way to copy data from a field in a table to another field in a different table but in the same app?
App Z
Table 1 Input A
Table 2 Input B
Would like to copy Input A into Input B. I have been playing around with code. So far in Input A when I type in the data in Input A it goes and pulls data from a Data App I created. Now I need to copy that data into Input B in Table 2. Thanks in advance.
(function() {
'use strict';
var PROD_TABLE = 'tbl_prod_info';
var DET_TABLE = 'tbl_prod_ship_details';
var BC_CODE = 'fg_bc_code';
var BC_NAME = 'fg_name_tbl';
var DET_BC_CODE = 'det_bc_code';
var DET_BC_NAME = 'det_name_tbl';
var tblEvents = [
'app.record.edit.change.' + BC_CODE,
'app.record.create.change.' + BC_CODE
];
kintone.events.on(tblEvents, function(event) {
var record = event.record;
var subtables = record[PROD_TABLE].value;
subtables.forEach(function(row) {
if (row.value[BC_CODE].value !== "") {
record[DET_BC_CODE].value = row.value[BC_CODE].value;
}
});
return event;
});
var formShowEvents = [
'app.record.create.show',
'app.record.edit.show',
'app.record.detail.show'
];
kintone.events.on(formShowEvents, function(event) {
event.record[DET_BC_NAME].disabled = true;
return event;
});
After thoroughly reviewing your script, I identified the following issues:
Row Synchronization: The script assumes that Table 2 (tbl_prod_ship_details) already has the same number of rows as Table 1 (tbl_prod_info), which may not always be the case.
Barcode Copying: It copies the barcode value from Table 1 (fg_bc_code) into Table 2 (det_bc_code), but only for a single field, not dynamically for each row.
Field Initialization: It tries to assign values to det_bc_code without ensuring that the field exists first, which can cause errors.
Read-Only Settings: It disables only a single field (det_bc_name) but does not apply read-only settings dynamically to newly added rows.
Fixes Implemented:
Automatic Row Synchronization: Table 2 now automatically adds or removes rows to match Table 1, ensuring they always stay in sync.
Dynamic Barcode Copying: Instead of copying just one field, the script now loops through each row in Table 1 and updates the corresponding row in Table 2 in real-time.
Proper Field Initialization:det_bc_code is now explicitly initialized whenever a new row is added, preventing errors and ensuring all rows have the correct structure before copying values.
Enforced Read-Only Fields: A function now ensures that all fields in Table 2 are set to read-only, even when new rows are added dynamically.
With these changes, Table 2 now dynamically syncs with Table 1, barcode values update instantly, and all fields in Table 2 remain read-only, eliminating errors and ensuring data consistency.
Here's an example script, please use it for your reference:
(function () {
"use strict";
var TABLE_1 = "tbl_prod_info"; // Table 1
var TABLE_2 = "tbl_prod_ship_details"; // Table 2
var BC_CODE = "fg_bc_code"; // Barcode field in Table 1
var DET_BC_CODE = "det_bc_code"; // Barcode field in Table 2
/**
* Adjust Table 2 row count based on Table 1 row count
*/
kintone.events.on(["app.record.create.show", "app.record.edit.show", "app.record.create.change." + TABLE_1, "app.record.edit.change." + TABLE_1], function(event) {
var record = event.record;
var table1 = record[TABLE_1] ? record[TABLE_1].value : [];
var table2 = record[TABLE_2] ? record[TABLE_2].value : [];
if (!table1) return event;
if (!table2) record[TABLE_2] = { value: [] };
var table1Length = table1.length;
var table2Length = table2.length;
// Remove extra rows in Table 2
if (table2Length > table1Length) {
record[TABLE_2].value.splice(table1Length, table2Length - table1Length);
}
// Add missing rows to Table 2 and ensure they are read-only
while (record[TABLE_2].value.length < table1Length) {
record[TABLE_2].value.push({
id: null,
value: {
[DET_BC_CODE]: { value: "", type: "SINGLE_LINE_TEXT", disabled: true } // Ensure read-only on creation
}
});
}
return event;
});
/**
* Dynamically copy barcode values from Table 1 to Table 2 in real-time
*/
kintone.events.on(["app.record.create.change." + BC_CODE, "app.record.edit.change." + BC_CODE], function(event) {
var record = event.record;
var table1 = record[TABLE_1] ? record[TABLE_1].value : [];
var table2 = record[TABLE_2] ? record[TABLE_2].value : [];
table1.forEach(function(row, index) {
var barcodeValue = row.value[BC_CODE] ? row.value[BC_CODE].value : "";
if (table2[index] && table2[index].value) {
table2[index].value[DET_BC_CODE].value = barcodeValue;
}
});
return event;
});
/**
* Ensure all fields in Table 2 are always read-only (including newly added rows)
*/
function setTable2ReadOnly(event) {
var record = event.record;
var table2 = record[TABLE_2] ? record[TABLE_2].value : [];
table2.forEach(function(row) {
if (!row.value.hasOwnProperty(DET_BC_CODE)) {
row.value[DET_BC_CODE] = { value: "", type: "SINGLE_LINE_TEXT" };
}
row.value[DET_BC_CODE].disabled = true; // Make read-only
});
return event;
}
// Apply read-only settings when the form loads and when rows are added
kintone.events.on(["app.record.create.show", "app.record.edit.show", "app.record.create.change." + TABLE_1, "app.record.edit.change." + TABLE_1], setTable2ReadOnly);
})();
Having an issue. When I add an item to Table 1 it will copy to Table 2 but when I add another row in Table 1 it throws an error. Now if I add an another row to Table 2 first manually (press the + at the end of the row) then it works. IT wont add rows to Table 2 to match Table 1
All the fields listed in the error do not need to be filled automatically. The user will fill them in, if that makes sense. Bascially the user will fill in Item Num in Table 1 and then that same Item Num will copy to a row in Table 2 but the user will need to fill in the rest of data in that row.