I'm using the Modern Forms Designer in Laserfiche Forms Professional Version 11.0.2311.50564.
I have a table where the first two columns are Employee Name and Employee ID. When the user enters the values in the first row, and then clicks on the "Add" button to add another row, I want the newly added row to have the same Employee Name and Employee ID. The user is still able to change those values, if necessary. This is just to make is easy for them to add multiple rows for an employee, but be able to add rows for multiple employees.
I have a hidden field (fieldId 16) where I store the number of rows currently in the table. The calculation for that field is below:
=COUNTIF(Table.Employee_Name, "<>") + COUNTIF(Table.Employee_Name, "=")
In the JavaScript code, employee name from the previous row is logged in the console, but it seems that the function runs twice every time a row is added. The screenshot below shows the console when I run it with the 2 setFieldValues() lines (line 14 and 15) commented out.
In the JavaScript code below, the setFieldValues() lines (14 and 15) cause an infinite loop.
const rowCountFieldId = { fieldId: 16 }; const handleRowChange = async () => { const getRowCount = LFForm.getFieldValues(rowCountFieldId); const newRowIndex = getRowCount - 1; //indexes start with 0 const priorRowIndex = newRowIndex - 1; // copy employee name and employee ID from previous row into new row let pEmpName = LFForm.getFieldValues({fieldId: 6, index: priorRowIndex}); let pEmpID = LFForm.getFieldValues({fieldId: 7, index: priorRowIndex}); console.log("Employee Name = " + pEmpName + ", newRowIndex = " + newRowIndex + ", priorRowIndex = " + priorRowIndex); await LFForm.setFieldValues({fieldId: 6, index: newRowIndex}, pEmpName); await LFForm.setFieldValues({fieldId: 7, index: newRowIndex}, pEmpID); } LFForm.onFieldChange(handleRowChange, rowCountFieldId);