I have a button that updates rows via a workflow and have a script after to check every 2 seconds on the updated data. I am able to retrieve the initial data and "see" the data being refreshed though in the code the data is not refreshing. Is there something specific I need to do to access the new data in the table?
async function updateDispatchStatus() {
// Reference to your dispatchStatus text field
const dispatchStatusField = dispatchStatus; // Assuming dispatchStatus is the name of your text field
// Step 1: Set status to 'In progress'
dispatchStatusField.setValue('In progress...');
console.log('Status set to In progress...');
// Step 2: Wait for 2 seconds
await new Promise(resolve => setTimeout(resolve, 2000));
console.log('Initial wait of 2 seconds complete.');
// Reference the initial total count
const totalJobs = tableScheduledDispatchRouteDetails.selectedRows.length;
console.log(`Total selected jobs: ${totalJobs}`);
// Calculate the initial counts
const initialNotScheduledSelectedCount = tableScheduledDispatchRouteDetails.selectedRows.filter(
row => row.is_scheduled === 'Not Scheduled'
).length;
const initialSuccessCount = tableScheduledDispatchRouteDetails.data.filter(
row => row.is_scheduled === 'Success'
).length;
const totalSuccessGoal = initialNotScheduledSelectedCount + initialSuccessCount;
console.log(`Initial Not Scheduled Selected count: ${initialNotScheduledSelectedCount}`);
console.log(`Initial Success count: ${initialSuccessCount}`);
console.log(`Total Success Goal: ${totalSuccessGoal}`);
// Loop to check the dispatch status until complete or maximum loops reached
let loopCount = 0;
const maxLoops = 5;
while (loopCount < maxLoops) {
console.log(`Loop ${loopCount + 1}/${maxLoops} - Triggering getScheduledDispatchRouteDetails...`);
// Trigger the query and wait for it to complete
await getScheduledDispatchRouteDetails.trigger();
console.log('Query completed.');
// Short wait to ensure table data refreshes
await new Promise(resolve => setTimeout(resolve, 500));
// Debug: Check the refreshed table data
console.log('Table data after query:', tableScheduledDispatchRouteDetails.data);
// Refresh the total success count
const tableData = tableScheduledDispatchRouteDetails.data;
const selectedRows = tableData.filter(row => row.isSelected); // Assuming `isSelected` tracks selected rows
const totalSuccessCount = tableData.filter(row => row.is_scheduled === 'Success').length;
console.log(`Updated Success count: ${totalSuccessCount}`);
console.log(`Updated selected rows:`, selectedRows);
// Check if all jobs are dispatched
if (totalSuccessCount === totalSuccessGoal) {
console.log('All jobs successfully dispatched!');
dispatchStatusField.setValue('Complete!');
utils.triggerConfetti(); // Trigger confetti animation when complete
break; // Exit loop
} else {
// Update the dispatch status text field with the progress
dispatchStatusField.setValue(`${totalSuccessCount} out of ${totalSuccessGoal} dispatched`);
console.log(`${totalSuccessCount} out of ${totalSuccessGoal} dispatched`);
// Wait for 3 seconds before checking again
await new Promise(resolve => setTimeout(resolve, 3000));
console.log('Waiting 3 seconds before rechecking...');
}
// Increment the loop counter
loopCount++;
}
if (loopCount === maxLoops) {
console.log(`Reached maximum loop count (${maxLoops}). Exiting...`);
dispatchStatusField.setValue('Stopped after maximum attempts.');
}
}
// Execute the function when the button is clicked
updateDispatchStatus();