Using tablefields

Viewing 1 reply thread
  • Author
    Posts
  • May 30, 2026 at 1:27 AM #54209

    Steve-Kai Vyska
    Participant

    Hello everyone,

    I’m currently experimenting with the “Table Fields”.

    When creating one, there is an option to select a form. If I choose an existing form, I can use the button **”Copy fields into table…”** to insert the fields title from the previously selected form into the table. This works perfectly. However, I’m a bit curious about the description text displayed there:

    > “Tap Forms will copy values from the selected records based on matching field titles.”

    Is this functionality only available through the form selection field, or is there a way to achieve the same thing via scripting?

    When I tried to do this with a script, I could only get it working by hard-coding the fieldsIDs beforehand. In other words, if I add a new field to the table, I have to manually add the corresponding field ID to the script.

    Is there any way to read the table column headers via javascript, so that the script could iterate through them and (since the names are identical) retrieve the corresponding values from the original form automatically by getFieldNamed(‘Name’)?

    Thanks for any ideas, and have a great weekend!

    Steve

    May 30, 2026 at 4:00 AM #54210

    Daniel Leu
    Participant

    You can use field.sortedTableFields to get field details of a table.

    Following function copies the values from the source record to a new row in the table of the current record. I’ve verified for text and number fields.

    /**
     * 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;
            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;
    }
    

    To verfiy, I use this sample script which just adds a random record to the table.

    function selectSourceRecord(formName) {
    	// return a random record from the form formName
    
    	const records = document.getFormNamed(formName).fetchRecords();
    	const index = Math.floor(Math.random() * records.length);
    	return records[index];
    }
    
    function Copy_Record_To_Product_Table() {
    
    	// source form name	
    	const sourceFormName = "Products";
    
    	// select a random record from the source form
    	const sourceRecord = selectSourceRecord(sourceFormName);
    
    	// table field name
    	const tableFieldName = "Products";
    
    	// get the table field
        const tableId = form.getFieldNamed(tableFieldName).getId();
    
    	// copy the record to the table
    	copyRecordToTable(sourceRecord, tableId);
    
        document.saveAllChangesAndRefresh();
    }
    
    Copy_Record_To_Product_Table();
    

    Let me know if this works for you!

    • This reply was modified 9 hours, 56 minutes ago by Daniel Leu.
    • This reply was modified 9 hours, 54 minutes ago by Daniel Leu.
    • This reply was modified 9 hours, 53 minutes ago by Daniel Leu.

    Cheers, Daniel

    ---
    See https://lab.danielleu.com/tapformspro/ for scripts and tips&tricks

Viewing 1 reply thread

You must be logged in to reply to this topic.