I want to compute pricing of an item which depends on certain factors.
I have tried following so far and have been facing issues in all of those:
Assigning value to a field of a DB thing in workflow: Bubble doesn’t allow any computation there
Run Javascript of Toolbox plugin: Facing an issue with the JS to Bubble element there
Use “Expressions” from Toolbox plugin: Facing some issues there too with fields being empty and me not being able to put conditions there effectively. Also, it was the ugliest way as I have to create many expressions and basically code full pricing logic using workflow boxes while actual JS code was just about 10 lines.
Use “Server Script” of Toolbox plugin: Some issue here too. Getting error of “properties.thing1.get not a function”
While it would be great if someone could guide me in fixing any of these issues so that I get unblocked, but I am pretty sure there is some official way that Bubble supports for doing such computations? It can’t be dependent on plugin created by someone?
Somehow I didn’t see the options of "+ - * " etc. that come in the dropdown.
What I was trying was to enter * itself and then it won’t move forward. That made me feel that * is not possible.
This one is also of course very limited, as looks like parenthesis are not allowed. But I guess I should be able to manage without that with some ugly work.
For parenthesis, just remember that the calculation is done left to right, so the next statement always evaluates the outputs of the previous statements. Keeping this in mind you can actually build long, complex calculations.
No. I think what @exception-rambler is saying is that there is no “conventional” operator precedence; nor can parentheses be used to enforce precedence. Instead, the expression is simply evaluated from left to right. So, for instance…
7 + 3 * 2
…would evaluate to 20 and not 13.
EDIT: Or are parentheses allowed? Hmm, that’s actually something I need to test. I might be wrong here.
Yeh it’s not possible unfortunately - it’s an ongoing ask on this thread: Order of operations
If I was doing your calculation @mghatiya I would personally follow the approach that I recommended here, because with a few lines of JS you can clear up some the edge cases - but it’s definitely not ideal that you can’t handle this more straightforwardly.
This raises an interesting question about what a “better” approach to mathematical expressions might look like in Bubble. IOW, what might be a more friendly UI for creating simple math expressions?
It seems, ideally, one would be able to simply type an expression using parentheses but somehow reference “variables” representing the inputs. Given Bubble’s focus on workflows and app logic, mathematical expressions are one place where Bubble’s UI “gets in the way”, and resorting to JS is far from ideal.
It would be cool to click a button “Calculate expression” and then have like a dial-pad/calculator widget thing popup. Then from there you can use it as a regular calculator, but also reference input values and custom states.
Maybe the current set up can’t handle parentheses because it could too easily be considered part of a string