I have two inputs (Quantity and Price) and when ‘save’ is clicked I want to save the quantity x price to a custom state [list] so I can later display the :sum.
The custom state is always asking for more info but why? Both the inputs and the data type are integers. The custom state is a list. What’s missing?
The custom state is a list… so it expects a list of numbers…
You’re trying to set it to a single number - hence the error.
I think I should be settings to a list but not sure how to do that, I don’t have the option to use the ‘each item’ function despite the custom state being a list.
For clarity, the user inputs as many items as they want and I will save these to the custom state. Then I will show the total (so I think I need a list right?)
To add to a custom state list you need to use ‘plus item’ after the existing list value…
i.e. create-estimate's _calculate: plus item: inputEstQuantitys' value * inputEstUnitPrice's value
But, bear in mind that (for most purposes) lists in Bubble can’t contain duplicate values… so your custom state list of numbers won’t be able to hold the same number more than once.
So, if you have, for example Item 1: 2 x £50
Item 2: 4 x £25
and Item 3: 10 x £10
your custom state list will only contain a single number (100), so the sum of that list will be £100, instead of the actual sum of the 3 items (£300).
To get around that issue, you can use a custom state list of Texts (instead of numbers), and ensure that each text in the list is unique (by using some unique identifier, followed by the price). Then you can spit the texts and convert them back into numbers to do the ‘total’ calculation correctly.
1 Like
Ok thank you @adamhholmes I think I understand, phew that is harder than I hoped lol… I will play with this and see if I can get it to work. Thank you for the clarification on duplicates, I didn’t know that.
I have it working with type text, but can’t figure out what identifier to use, can you suggest anything?
You could just use a simple number, corresponding to the list item number…
Or, in your case, you could use the Item name
Or, if you’re using items from the database, I’d use the unique ID of the item.
Good idea, I can use the description, however how do I add the price to the end of the description in the formular?
Thank you so much for taking the time to create that example, I’ve learned a lot and I like the alert you added for duplicates. I have it all working but saving the data to the database is complicated because I have two custom states, “calculate” which folllows your example, and “lineItems” which should save the data to the Datatype Line Items. I am still working on this but wanted to thank you for the last post.
@adamhholmes I ended up using a combination of your solution with something else but have it working exactly as I wanted now. Thanks again for your help, I am constantly struck by the amount of Bubble knowledge you have and your generosity with your time.
1 Like