[SOLVED] Simple custom state to store info?

Hi all,

Instead of saving temporary data to the database, I want to store it in a custom state and display the results on the next page.

On Page 1 you can save a fruit name and colour.

The main index page has a custom state called _tempFruit, so intuitively I want to send the input field data to the custom state, _tempFruit.

But, I am only able to save one input field to the custom state, either name or colour, but not both?

Could anyone take a look and see what I am missing please, feel free to edit the project. Thank you!

1 Like

You can create another custom state:

image

Do I need to create another one? I thought one custom state can contain various bits of info?

For example, on my main app I have a custom state for invoice ‘line-items’ and that contains many different bits of data (price, quantity etc). I got help to create that set up so I am unsure how it was achieved.

I’d like to know how we are ‘supposed’ to do it, the best practice way to store info in a custom state and refer to it on another group? I have gone through so many tutorials and documentation but they mainly talk about using custom states to show or hide elements.

Now I see what you want. Yes, you can.

To make it simple…

Basically, a custom state is a variable that can store things like text, number, date or an entire data type of your database.

A Data type is an iten from your database. So it will contain all fields of you data type stored in a single custom state…

Let’s say you have a table (data type) called Users. And in this table you have name, email, phone, and birthdate. If you save a specific User as a custom state, you will save all this users’s data inside this custom state (name, email, phone and birthdate). But to do it, you need to tell Bubble that this specific custom state is an User instead a text or a number.

In your case you need to change your custom state’s type from “text” to “Fruit”.

Thank you @rpetribu for the explanation. If I understand correctly, you are saying that I need to have a datatype in order to store the data in a custom state. If that is the case, I would need to add a data type “Fruit” and (as you say) change my custom state’s type from “text” to “Fruit”.

But, doesn’t this defeat the purpose of using a custom state to avoid the need of saving things to the database?

Maybe I should back up and explain my actual use case.

The reason for me wanting to learn this is because on my main site my users fill in some form fields to generate a PDF invoice. That invoice does NOT need to be stored in the database, but I do want to show it (one time) when they click Preview. Currently I save the PDF to the database, display it to the user when they click Preview and then later I will delete it from the database.

This seemed ‘clunky’ so I was hoping I could save the PDF in a custom state and display it without it ever being saved to the database.

Is this type of thing possible in Bubble?

Might be worth watching this. I think it more or less does exactly what you describe https://youtu.be/kC87wWhrWSU

Store an entire User as a custom state can be useful for a lot of things like, as an example, have all the info about this user cached in your browser, what eliminates the necessity to make more queries in your database. This makes your app much faster.

But if you don’t have the invoice already saved in your database, you don’t have any invoice to be stored as a custom state, make sense? What you have is some texts and numbers that, together, will compose this specific invoice. In this case, your only option is to store the texts as text and the numbers as number and, in the end, you will be able to retrieve and display this info in some invoice template (without ever have needed to actually save this invoice in our database).

You are doing this in order to be able to show an invoice template (filled with some data) at the end of the process, am I right?

Thank you but I have already managed to create the invoice. I did actually follow that video last week, it was good but I found a lightly different solution. No, this thread really related to displaying the PDF (either in a popup or new group) without saving it to the DB.

That makes sense.

Ok, this is where I may have gotten confused because I thought a custom state could store any information, regardless of if it’s in the DB or not.

That’s correct. The ‘save invoice’ button generates an invoice using PDF Conjurer. The final step in the Workflow is to save it to the DB. This works fine but, as I said, after a user has previewed their newly created invoice, I don’t want it saved to the DB.

I thought I could save it to a custom state and show that to the user instead. In fact, the plugin owner suggested this solution to me in a separate thread.

And it can!

Text, numbers, dates… including lists…

And, if you want, you can also store a whole line of your database (called, “data type” - User, Invoice, Car, Fruit… anything). But off course, to store all the information about a line, you need this line saved in your database :sweat_smile:.

To do what you desire, just keep saving the texts and numbers in different custom states and, in the end, retrieve this information to fill the invoice.

Ok, I think I get it now!

So, going back to the original task of asking a user to input a fruit name and colour and then storing that info in a custom state before finally displaying that info somewhere else, I would need two separate custom states, one to save the name and one for the colour. Is that correct?

I further understand from what you’ve said that if I did save to the ‘Fruit’ data type then I would have the advantage of only needing one custom state.

As reference, here is the actual thread post where I respond to the creator of PDF Conjurer explaining that I can’t get his suggestion of a custom state to work. Page to PDF converter question - #19 by darren.james7518

That’s it!! :+1:

You can create multiple custom states to store how much texts/numbers do you need.

1 Like

Thank you, I appreciate your patience! I have just set two custom states on my dummy webpage and the fruit info displays on the second page. I feel a bit more confident now that I am doing things correctly now so thank you again for your help.

1 Like