What's the best way to save this data..?

Hi,

I need to save some data to the database, but first I am saving it to the page’s custom state. To simplify things, imagine I have to save an animal, it’s food option and portion size, like this:

dog

Assume each option is selected via a radio button and saved to the custom state. Also, the options are all separate Option Sets. My question is, how do I save this? I seem to have a mental block, for example, if someone clicks ‘Dog’, I need to instantly save that to the custom state. Then, if they click Fruit, how do I associate that with the ‘Dog’ which was just selected?

If you really don’t want to touch the database, then you can just create a string (text) containing references to the various elements you’re working with (in this case, as you’re using Option Sets, you can use the Display of each).

So, create a custom state (text) and simply set the state to be a text that contains the name (Display) of each item… something like this

Dog:Fish|Large/Small,Meat|Large/Small,Fruit|Large/Small

Then you can access the various elements when you need them by manipulating the text - e.g.

split by :first item (to get the Pet)
split by : last item (to get Fish|Large/Small,Meat|Large/Small,Fruit|Large/Small)
Then split that by , to get each food and portion pair (e.g. Fish|Large/Small)
Then split each of those by | to separate the Food from the quantity

and obviously match those to the Options in your Options set.

Of course, if you need the custom state to be a list, rather that just a single text, then you can do the same thing with a list of texts.

1 Like

That’s a brilliant solution, thank you!