Category: Tips & Tricks
Copying a Record into a Table Field
When a form contains a Table field, you sometimes want to programmatically append a new row and populate it from an existing record. The helper function below does exactly that: it finds all fields in the table whose names match fields in the source record and copies their values across.
Pass the source record and the field ID of the Table field. The function resolves the table field, builds a name-to-field map from table.sortedTableFields, appends a new row using addNewRecordToField, and then walks the source record's fields to copy every matching value.
/**
* copyRecordToTable(srcRecord, tableId)
*
* Copies values from `srcRecord` into a new row appended to the table field
* identified by `tableId`, for all fields whose names match between the
* source record's form and the table.
*
* @param {TFRecord} srcRecord - The source record to copy values from.
* @param {string} tableId - The field ID of a Table-type field on the record.
* @returns {TFRecord} - The newly created table row record.
*/
function copyRecordToTable(srcRecord, tableId) {
// Resolve the table field from the record's own form using the field ID.
const table = form.getFieldWithId(tableId);
// Get the fields defined on the table, keyed by name for fast lookup.
const tableFields = table.sortedTableFields;
const tableFieldsByName = {};
for (var i = 0; i < tableFields.length; i++) {
var tf = tableFields[i];
tableFieldsByName[tf.name] = tf;
}
// Add a new row to the table.
const newRow = record.addNewRecordToField(tableId);
// Walk every field on the source record's form and copy matching values.
const sourceFields = srcRecord.form.fetchFields();
for (var j = 0; j < sourceFields.length; j++) {
var sf = sourceFields[j];
var match = tableFieldsByName[sf.name];
if (match) {
var value = srcRecord.getFieldValue(sf.getId());
newRow.setFieldValue(match.getId(), value);
}
}
return newRow;
}
Example usage — copy the current record into a Table field and save:
const tableId = 'fld-xxxx'; // update to your Table field ID
copyRecordToTable(myRecord, tableId);
document.saveAllChanges();
Only fields with matching names are copied; unmatched fields in either direction are silently skipped.
Please note that the function was only verified for text and number fields.
Author
- Daniel Leu, info@danielleu.com
Do you like this script? Are you using it? Please consider supporting me by buying me a coffee!
Thanks!
Last modified: May 30, 2026 1:38:23 PM
