Reset the value of a given variable to 0 for all records

Hi there!
I’m using client-side storage to store a list of “thoughts” (strings) the user inputs one by one. I’ve watched the tutorials and trawled through the forum (particularly through the “iterating through arrays” thread) and I can’t get the results I want. What I’d like to do is…

  • each record has an “Upvoted” numerical value that the user can change for each record one by one. I’d like to be able to set this value to 0 for every record in the database with the tap of one button. I’ve been wrangling with the SELECT and MAP operators, but to no avail.
  • I’d like to be able (ideally) to import a list of “thoughts” (records) from a CSV file and export all the records in the app to CSV, or if that’s problematic then I’d at least like to be able to paste a long list of thoughts from the clipboard into an input box and have each new line create a new record in the database, and then I’d like to be able to output all the thoughts currently in the database in a way that could be saved to the clipboard and pasted into a document. Either of these solutions would allow the import and export of data from the app. Thanks for any help you are able to provide, and let me know if I’m not explaining that clearly enough…

Loops are a bit tricky to do currently, and we don’t have Batch update records type of flow function.

So, we want to run Update record for every record in our client-side data collection, with each having the same payload {upvotes: 0}, but with the ID different each time. If we have all our records in data.Thoughts data variable, we can add looping logic like this:

To break it down:

  1. We set pageVars.currentLoopIndex to 0.
  2. The If condition checks the formula pageVars.currentLoopIndex < COUNT(data.Thoughts) – if the current index is less than the number of records, we keep going.
  3. We run Update record, fetching the record ID for the current loop with the formula LOOKUP(PICK_ITEM(data.Thoughts, pageVars.currentLoopIndex), "id") and passing the update payload of {upvotes: 0}
  4. We set the value of pageVars.currentLoopIndex to pageVars.currentLoopIndex + 1
  5. Back to step 2

In a similar fashion, once you have your source data, you can create a loop that creates a new record instead of updating it.

However, especially if your data is not very complex, you can consider using the Set item to storage and Get item from storage to store/fetch just an array of data and set it to e.g. an app variable to be used elsewhere in the app.

As for the import/export – CSV file could be picked with Pick file and read as a text string with Read file, or you could paste your CSV content in a multiline box and bind it to variable, then read that variables’ contents. You could then run the string through a JavaScript node to parse the CSV into JSON.

If your data is 1-dimensional – just one line per thought title – you could use SPLIT and MAP to create an array of thought objects:

MAP(SPLIT(pageVars.textImport, "\n"), {thought: item})

\n is the newline character, so your input is split into an array of strings, one per new line.

Exporting would be doing this just in the opposite direction – joining an array of data into a single string with newlines.

2 Likes

Thanks Harri, I really appreciate your time on this.
I ended up implementing a copy and paste method of import and export using the MAP(SPLIT( JOIN and “\n” functions. Just in case anyone else comes across the same problem, here is what worked for me:

EXPORT TO CLIPBOARD:


Page variable ArrayOfThoughts: PLUCK(data.NewThoughtItem, “Label”)
Page variable ThoughtsOnNewLines: JOIN(pageVars.ArrayOfThoughts, “\n”)

IMPORT FROM MULTI-LINE INPUT (one item per line)


Multi-line input saved to page variable ListOfThoughts
Page variable ListOfThoughtsAsArray: MAP(SPLIT(pageVars.ListOfThoughts, “\n”), {Content: item})
Then the rest of it is pretty much what you suggested, with a page refresh at the end.