The following code works, but I'm trying to figure out how to store the table cells in variables. So, if the fieldID for the table column changes, I would just have to change the fieldId on one line of code.
Code that works without error:
const formFields = { vacColFieldId: { fieldId: 102 }, vacAccum: {fieldId: 102, index: 0}, vacEarned: {fieldId: 102, index: 1}, vacTaken: {fieldId: 102, index: 2}, vacBalance: {fieldId: 102, index: 3} }; const updateBalance = async (formFields) => { let VacationAccumulated = LFForm.getFieldValues({fieldId: 102, index: 0}); let VacationEarned = LFForm.getFieldValues({fieldId: 102, index: 1}); let VacationTaken = LFForm.getFieldValues({fieldId: 102, index: 2}); let VacationBalance = LFForm.getFieldValues({fieldId: 102, index: 3}); let result = (VacationAccumulated + VacationEarned) - VacationTaken; if (VacationBalance !== result) { await LFForm.setFieldValues({fieldId: 102, index: 3}, result); } } LFForm.onFieldBlur(function() {updateBalance();}, formFields.vacColFieldId);
Code that doesn't work:
I've tried to change the "if (VacationBalance !== result)" section to the following, but it breaks the functionality:
if (VacationBalance !== result) { await LFForm.setFieldValues(VacationBalance, result); }
The error message in the console reads as follows:
Uncaught (in promise) Error: Unable to find field. Please check your ID.
When I try to print the VacationBalance variable to the console, it doesn't return anything:
console.log("VacationBalance = ", VacationBalance);
Result shown in the console --> VacationBalance =
Also, I need to do the same calculation in the 2 columns adjacent (fieldIds 103 and 104) to the Vacation column (fieldId 102). So, it would be nice to pass in the fieldId to the updateBalance() function.
Any suggestions for improvements in this code are much appreciated!