Table data not refreshing via code

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();

Hi @Shawn_Optipath :wave:

When you're referencing values in your query that are changing while the query is running, you need to enable this setting on the advanced tab:

I believe that should mostly unblock the query :crossed_fingers: I also noticed the utils for confetti should just be utils.confetti().

In case it is helpful for you or other users following along, an similar solution instead of the JS query could be to add a response block to your Workflow which returns in the app once the Workflow has completed.

Thanks for the detailed post. Please let me know if this Advanced query setting doesn't fully resolve!

Thanks @Tess!

I wish I knew about that setting before! I just had a look and don't see it. Hmmm.

I ended up accessing the refreshed data by creating another query and returning the values needed.

Thanks for the confetti help :slight_smile:

I couldn't use a response block in this case because the workflow lasts roughly 30 seconds and I needed a way to update the user at shorter intervals. Unless you can somehow use a response block in a loop for updates? That would be useful.

The setting is only on Javascript queries. Glad you have a solution though!

Makes sense on the shorter intervals. I think this is a good approach in that case. I believe the loop responses would post to your app at the end, rather than as it loops. We have a related feature request in our backlog for polling the workflow status via API. I will update this thread if it gets prioritized.

1 Like