Issue with Updating Multiple Documents in Retool with Firestore

Hi,

I'm new to Retool, and I'm currently experiencing an issue with updating multiple documents in Retool using Firestore.

Here's a brief overview of the issue I'm facing: I have created a Retool table that displays data from a Firestore collection, and I have enabled multi-row selection to allow users to select multiple rows.

My goal is to update multiple documents in Firestore by clicking a button in Retool, but I'm having trouble figuring out how to do this.

Here are the steps I've taken so far:

  1. Created a Retool table that displays data from a Firestore collection
  2. Enabled multi-row selection in the Retool table
  3. Created a button in Retool that triggers a query to update a single document in Firestore
  4. Tested the query to update a single document, and it works correctly
  5. Attempted to modify the script called when clicking on the button to first, retrieve the ids of the selectedRows in the Retool table and then call the query for each id in order to update multiple documents.

This is the script used:

const selectedRows = Exercices.selectedRow.data

const selectedIds = selectedRows.map(row => row.id) 

const updatePromises = selectedIds.map(id => {
  return query9.run()
})

await Promise.all(updatePromises)

However, I'm having trouble modifying the query. What should I put in the document Id and value fields?

I would greatly appreciate it if you could provide any guidance or assistance in resolving this issue. Please let me know if you need any additional information from me.

Thank you for your time and assistance.

Hey @squero!

Can you try using query9.trigger({additionalsScope: {id}}) instead of query.run() in your script, and then replacing {{Exercises.selectedRow.data['0'].id}} with just {{id}} in the settings for query9?

There are some more docs here that might be helpful! Let me know if that works :slightly_smiling_face:

Thank you for getting back to me.

I have made an attempt to modify the query, which now appears as follows:

const promises = editMultipleExercicesTable.recordUpdates.map((row) => {
  return updateExercice.trigger({
    additionalScope: {
      document_id: row.id,
      document_object: {
        disabled: row.disabled,
        objective: row.objective,
        i18n: {
         fr: {
          title: row.title,
          instructions: row.instructions      
         }   
        }
      },
    },
  });
});

return Promise.all(promises);

As you can see, I am calling the updateExercice Firestore query shown in the screenshot below:

Finally, I'm triggering query9 in my table which contains the recordUpdates:

Despite this, I have been unable to update the documents in the Firestore database.

Could you please assist me in identifying where I may have made an error?

Thank you.

Also, when I run the query separately it works fine. So I'm assuming that I'm not calling it the right way in the table component. :thinking:

Would you mind posting a screenshot from your Debug Tools that shows that query being called? We might be able to inspect the additional scope there:

Also curious to see what is returned by the updateExercise query :thinking:

As another thought, you mentioned triggering the query from selectedRow originally but this seems to trigger from recordUpdates - was there a change in your intended workflow?

Yes, I have made changes to my initial workflow.

I have a table that displays all the exercises in my collection. However, since it may contain thousands of rows, I would like to allow users to select the lines they want to update.

To achieve this, I have implemented a functionality where users can select the rows they want to update and then click on a button called "Edit Multiple Exercises". This opens a modal containing a new table with only the selected rows, which the user can then edit. Once the user saves the changes made in this new table, it updates the original table.

When running the handleUpdate method separately, outside of the table, the updateExercice query is called first, followed by the handleUpdates JavaScript function.

However, when calling it inside the table, only the handleUpdate function is called, and not the updateExercice query.

I am wondering if I need to set the trigger of the handleUpdate method differently in order to fix this issue.

Please let me know if you require any further information.

I was able to resolve the issue. It turns out that the reason why the updateDocument method was not being called was because it was retrieving an empty array.

I realized that this was due to another method being called when the "Save Changes" button was clicked, which was clearing the selectedRows and, as a result, the recorded updates...

Thank you for your support and assistance throughout this process.

1 Like