Create an object with custom state

Is it possible to store an object in a custom state? I’m using a workflow to trigger a button that creates some random records in the database, like ‘name: X, age: Y, …’. However, I’d like to create these records temporarily and only save a few of them to the database later. I thought using custom states would be a great approach since I’d only interact with the database when I’m ready, which would reduce the number of database calls.

Creating the item in custom states temporarily, meaning creating that “item” in a custom state before sending it to the database, isn’t possible. You’d need to have separate custom states for each value, or store everything as a JSON inside a single custom state and then, when saving it to the database, split the values from it.

Also, keep in mind that custom states only persist during the current page load. If you set something in them and then refresh the page, the states will be reset.

To actually save the data on the user’s device before sending it to the database, you could store this data in localStorage.

You could create a draft field in the database and create the records with the draft set to yes.
When you finish, you would change the draft state to no only for the records you are going to use, and you can clean up the rest from your database with an action, a scheduled API workflow, or a recurring event.

I did some research, and I think I’ll create a custom state for each value. It will be around 30 variables, but that should be better for performance since the user might trigger the flow multiple times, and it will only interact with the database when sending the final result. Currently, I’m creating the record in the database, modifying it to show to the user, and then deleting it after each trigger

In terms of data consistency I think @Isaque_Galvao solution is the much better one. Writing to db doesn’t take up too much time as far as I understand.
Especially when the user has to enter 30(+) values, you don’t want them being frustrated when they accidentally refresh or similar.

I understand, but the user won’t enter the values, they will be created automatically by a workflow trigger.

You can build a JSON, store it in state and use Backend WF to send it when it’s ready.

3 Likes

How exactly?

This depend of your actual layout/process.

Can you give more details about how your app is set, what you need to save exactly. Is it a list of just a single object? For single object, I don’t see why you would use state in this case. Is it because you want to keep values while user navigate to a different page? When you say save a “few of them to the DB later”… what do you mean?