How can I copy data into the datastore in page load?

This is another “I’m sure this is simple” scenario but I’m struggling to come up with the solution.

I have a SC script which returns data which I want to be copied into the datastore. What’s the best method for doing this?

Could add a dynamic event on app ready. In the app structure click on app, select dynamic event, select app, and then select on ready or load, then define your action.

Yes, that’s how I would do it but what should the action be? Should it be a flow to work through the records adding them one by one?

Add the server action (assume its a repeat) that is pulling your data, set no auto load, trigger with the on ready/load event and store to the datastore? Not sure what data you are loading or where it is coming from obviously but if from a server action that should work fine without the need for flows etc.

I get the theory but can’t get my head around actually implementing it!

I am using a datastore to just hold some record IDs which I use in parts of the overall system. When a particular page loads, I want to empty the datastore and fill it again with fresh data.

How would I simply create those records in the datastore?

On load clear the datastore, on ready update/insert records from the action to the datastore.

My fault obviously had not defined datastore schema hahaha (hence the edit).

Hmm. I’m trying to do that but the field to insert is always a single record (with [0] in it) so it’s not inserting all the records. This is why I’m thinking I should possibly use a flow so I can use a repeat but need to know that’s the best technique.

I’ve now tried to use a flow with a repeat to achieve this but whatever I try, the output fields is always empty:

I do this a lot…on success of the server connect that holds the data, run a flow. In the flow do a repeat of the recordset. Inside the repeat, add a run action that does the insert/upsert to the datastore.

Thanks @mebeingken. That’s exactly the process I’m trying to implement. I create the repeat within the flow and set the expression to the data from the SC but I can’t then access the fields when filling in the Insert to Datastore action.

Hmm. If you can see this data in dev console, then you might just type them in manually in the mini-code view.

Hmm, I can’t see anything relating to that in the dev console. Where should I see it? I’m normally looking for responses from SC files but I’m not sure if I’ve ever needed to look for flows.

Instead of prolonging the processes, you can activate the cache feature and see everything from the server connect in the local state. In this way, you will write less code and you can reach the result faster.

Ekran Resmi 2023-02-22 13.49.25

You can manually code ds1.insert(sc1.data.query1)
Expecting here that you query1 has same schema as defined in your ds, just so you don’t have any confusion when using pickers.

It will work even if schema does not match.