The function is approximately 80% functional, but after multiple tests, I have identified an issue where the date values are not being stored in the table. All other values are stored correctly.
I am unsure of the exact point in the code where this issue occurs. As I am new to coding in Laserfiche, I would appreciate some guidance in troubleshooting and resolving this problem.
JavaScript Code:
// Function to populate the other table based on matching indexes and amounts
function populateOtherTable(matchingIndexes, tableAAmount, tableAStatus, matchingDates) {
let invoiceAmountArray = [];
LFForm.enableFields({fieldId: 33});
// Retrieve existing invoice numbers and amounts from fields, ensuring they are valid values
const existingInvoices = ensureArray(LFForm.getFieldValues({ fieldId: 16 })).filter(value => value);
const existingAmounts = ensureArray(LFForm.getFieldValues({ fieldId: 101 })).filter(value => !isNaN(parseFloat(value)));
// Combine existing invoices and their amounts into an array
existingInvoices.forEach((invoice, index) => {
invoiceAmountArray.push({
invoiceNumber: invoice,
amount: parseFloat(existingAmounts[index])
});
});
console.log("Existing Invoice-Amount Array: ", invoiceAmountArray);
LFForm.enableFields({ fieldId: 8 });
const Table = ensureArray(LFForm.getFieldValues({ fieldId: 8 }));
// Delete all existing entries from TableC (reset the table)
const indicesToDelete = Array.from({ length: Table.length }, (_, i) => i);
LFForm.deleteSet({ variableName: "TableC" }, ...indicesToDelete);
setTimeout(() => {
// Retrieve invoice numbers from TableA
const tableAInvoice = ensureArray(LFForm.getFieldValues({ fieldId: 14 })).filter(value => value);
console.log("Matching Indexes: ", matchingIndexes); // Log the matching indexes
// If there are any matching indexes, proceed to match invoices and amounts
if (matchingIndexes.length > 0) {
// Map the matching invoices and amounts based on the indexes provided
const matchingInvoices = matchingIndexes.map(index => tableAInvoice[index]).filter(value => value);
const matchingAmounts = matchingIndexes.map(index => parseFloat(tableAAmount[index])).filter(value => !isNaN(value));
console.log("Matching Invoices: ", matchingInvoices);
console.log("Matching Amounts: ", matchingAmounts);
// Loop through the matching invoices to check they are already in the array
matchingInvoices.forEach((invoice, index) => {
const matchingAmount = matchingAmounts[index];
// Check if the invoice is already present in the Array
const invoiceExists = invoiceAmountArray.some(item => item.invoiceNumber === invoice);
if (!invoiceExists && matchingAmount) {
// If the invoice doesn't exist, add it to the array
invoiceAmountArray.push({
invoiceNumber: invoice,
amount: matchingAmount
});
} else {
// If the invoice already exists or the amount is invalid, log a message
console.log(`Invoice ${invoice} already exists or invalid amount. Skipping.`);
}
});
console.log("Final Invoice-Amount Array After Matching: ", invoiceAmountArray);
} else {
// If no new matches, keep only the existing values
console.log("No new matches found, keeping only existing values.");
}
// If there are any updated rows, add them to TableC
const updatedRowCount = invoiceAmountArray.length;
if (updatedRowCount > 0) {
LFForm.addRow({ variableName: "TableC" }, updatedRowCount - 1);
}
console.log("Added rows to TableC: ", updatedRowCount);
}, 3000);
setTimeout(() => {
// Map and retrieve the updated invoices, amounts, and dates
const updatedInvoices = invoiceAmountArray.map(item => item.invoiceNumber);
const updatedAmounts = invoiceAmountArray.map(item => item.amount);
const updatedDates = matchingIndexes.map(index => matchingDates[index] || null); // Default to null if undefined
console.log("Updated Invoices: ", updatedInvoices);
console.log("Updated Amounts: ", updatedAmounts);
console.log("Updated Dates: ", updatedDates);
// Set the updated values for invoices, amounts, and dates in the relevant fields
LFForm.setFieldValues({ fieldId: 16 }, updatedInvoices);
LFForm.setFieldValues({ fieldId: 101 }, updatedAmounts);
// Set the dates only if all dates are valid
const validDates = updatedDates.filter(date => date !== null);
if (validDates.length === updatedDates.length) {
LFForm.setFieldValues({ fieldId: 12 }, updatedDates);
} else {
console.warn("Some dates are missing or invalid.");
}
// Clear the matching indexes to reset for the next cycle
matchingIndexes = [];
LFForm.disableFields({ fieldId: 8 });
LFForm.showFields({ fieldId: 8 });
LFForm.disableFields({ fieldId: 33 });
LFForm.showFields({ fieldId: 95 });
LFForm.showFields({ fieldId: 33 });
}, 5000);
}
Output: