Condition based Math Needed

How do I do the following in a backend workflow? What plugin do I need and how do I implement it?

if(dType.subVal1 == OptionA) {
a = do a search for (expression1)
}else { a = do a search for (expression2) }

if(dType.subVal2 == OptionB) {
b = do a search for (expression1)
}else { b = do a search for (expression2) }

Make Changes to a thing (action)
dType.subVal3 = a + b;

In Python, I would code it like this:

## used for pretty code 
defaultIntValue = array()
user = ''

def doAsearchFor(*args):
    print('searching...')
    return

def getOptions(*args):
    print('getting an option...')
    return 

## actual data
patApp = 'current dataType with all needed fields'
stateFee = 0
ccFee = 0
Pay4Doc = 0
paymentAmount = 0
filingFee = 0

## figure out the numbers
if (patApp.InsuranceType == None):
    stateFee = doAsearchFor('selected state full fee')
else:
    stateFee = doAsearchFor('selected state discounted fee')


if (patApp.ApplicationType == getOptions('Marketplace')):
    filingFee = doAsearchFor(defaultIntValue('MarketplaceBaseFee'))
    Pay4Doc = doAsearchFor(defaultIntValue('MarketDocPay'))
else:
    filingFee = doAsearchFor(user.BasePay)

paymentAmount = stateFee + filingFee
paymentFee = paymentAmount * 0.29 + 0.30

if (patApp.ApplicationType is not getOptions('Marketplace')):
    Pay4Doc = paymentAmount - paymentFee - doAsearchFor(defaultIntValue('LocalRemoteAppFee'))

# Save all of the data 
patApp.PaymentAmount = paymentAmount
patApp.PaymentFee = paymentFee
patApp.Pay4Doc = Pay4Doc

##              Example 1   Example 2   Example 3
##              Market/Ful  Remote/Ful  Remote/Disc
"""     
FilingFee       $80         $75.70      $75.70
State Fee       $104.30     $104.30     $22.50

PaymentAmount   $184.30     $180.00     $98.20
PaymentFee      $5.64       $5.52       $3.15
Pay4Doc         $20         $50.18      $52.55     

"""

Hi @john30
With javascript should be pretty simple to achieve that (if I understand your problem correctly).

Create your custom plugin at Bubble | Build Powerful Full-Stack Apps Without Code

Open the actions tab (left side) and create a new one. Select server-side action type (as you said, you want to use this in the backend).

Add 8 new fields, 4 (where you compare data: dType.subVal1 equals to OptionA, and dType.subVal2 equals OptionB). The other 4 fields (where you will insert your dynamic "do a search for "ā€¦ for a1, a2 and b1, b2)


And create one ā€œreturned valuesā€ field that will be used to return the result (val3) and will be accessible after the workflow by step x ā€¦ result:

Now the code:

let val1 = properties.val1;
let optionA = properties.option_a;

let val2 = properties.val2;
let optionB = properties.option_b;

let search_a_1 = properties.a_1;
let search_a_2 = properties.a_2;

let search_b_1 = properties.b_1;
let search_b_2 = properties.b_2;

if (val1 == optionA) {
    a = search_a_1;
} else {
    a = search_a_2;
}

if (val2 == optionB) {
    b = search_b_1;
} else {
    b = search_b_2;
}

let result = a + b;

return {
    result: result
}

Captura desde 2022-05-18 19-29-38

Optionally you can skip the lines from 1 to 11 where the values of the searches are being declared. You can use the properties.xx directly in the code (but I think itā€™s easier to read and modify this way).

Redu.

@yusaney1 has presented a solution. The solution however involves creating a custom plugin which I believe is quite a bit of overhead and work to just to add the logic you presented in your question.

Iā€™m sure there is a purely Bubble solution that someone could present. The part that is a bit tricky to work with in Bubble presently is combining pieces of logic. Coming from a technical background, as you seem to be having presented Python code, one of the first plugins I add to every project is the ā€œToolboxā€ plugin. The toolbox plugin allows us to inject javascript into our Bubble app as needed. A small amount of scripting, in the right places, can go along way to creating workflows that are not convoluted with Bubble ā€œwork aroundsā€.

Keep in mind, the entire point of using Bubble is to write as little code as necessary. In other words, try to implement as much logic as you can within the Bubble workflows.

With that being said, I would use a combination of ā€œonly whenā€ workflow logic and a small bit of Javascript, using the ā€œServer Scriptā€ action from the Toolbox plugin, to accomplish your goal.

Here is an example setup of how to use only when and get a property value from an object.


What is happening inside of that Node script box, Iā€™ll mostly leave for you to search the forums and do some reading. However, as it pertains to this question, you will need to understand how to grab the property from your object. Bubble basically stores the properties as the field name, suffixed by the property type. So my person data type has a field named age and it is defined as a number, hence ā€œpersons[0].age_numberā€. However, if you want to be sure of the properties on your data type you can always send the properties out to a text object on your page for debugging using JSON.stringify(persons);

Finally, you can chain these together to create the logic you need above using the "Result of previous step like this:

Hopefully this gets you on your way.

1 Like

Thank you @yusaney1
I was trying to avoid using a plugin because, if I was going to use a plugin, Iā€™d want to just develop one that allows no-coders to do this kind of work but that level of plugin development on bubble is above my head (let alone outside of my available free time for coding projects).

Then I would go for a solution involving for example a Server script from @mishav Toolbox plugin as @bubble.trouble suggested, maybe with a different approach.

Good luck!

Thank you @bubble.trouble
Iā€™m going to wait to see if someone has a non-code version of what I want to do. If I donā€™t get any no-code answers, Iā€™ll do it in code. Thank you for your reply. I definitely have Toolbox installed as I do a lot of other tweaks that canā€™t be done in the no-code environment.

When you say expression1, expression2 etc. are you referring to conditions that would apply to a search?

i.e. with if(dType.subVal1 == OptionA) { a = do a search for (expression1) }else { a = do a search for (expression2) } you are just looking to set a to one of two possible values, dependent on OptionAā€™s value?

Same for b. And then you want to add them?

You should be able to handle that with a single expression in Bubble, no states required (which would be an issue on the backend). This is what I would do:

  • Test for your condition with an expression that resolves to true / false (yes / no in Bubble)
  • Format that as text
  • Place your Searches in the Yes / No sections
  • Convert the text resolution of that Boolean to a number
  • Repeat and add the both together

@exception-rambler
Thank you for your reply. Unfortunately, thereā€™s an ā€œIFā€ that comes before the option to format as text, which didnā€™t allow this as an option. Bubble needs to add if statements to their conditions and it will resolve this. For now, I went ahead and deployed the solution on a backend DB trigger that watches the parameters used to determine the outcomes of my pricing and then uses javascript to output a list of values, which I then assign in a second action.

Can someone help with my final problem???

Hereā€™s my code:
var insType = ā€˜Medicaid / Soonercareā€™;
var appType = ā€˜Localā€™;
var stateFullFiling = 104.3;
var stateDiscFiling = 22.5;
var marketplaceBaseFee = 80;
var remoteBaseFee = 75.7;
var docMarketPay = 20;
var localRemoteAppFee = 20;

// Above this line are input variables into the script in case you want to test the code ( I have ) outside of Bubble.  I don't get errors outside of bubble.
// Below this line is the full script copied to the table

var stateFee = 0;
var filingFee = 0;
var paymentAmount = 0;
var ccFee = 0;
var pay4Doc = 0;

if (insType === ""){
    stateFee = stateFullFiling;
}else{stateFee = stateDiscFiling;}

if (appType == "Marketplace"){
    filingFee = marketplaceBaseFee;}
else{ filingFee = remoteBaseFee;}

paymentAmount = stateFee + filingFee;
ccFee = Math.round(((paymentAmount * 0.029 + 0.30)) * 100) / 100;

if (appType == "Marketplace"){
    pay4Doc = docMarketPay;}
else{
    pay4Doc = Math.round((paymentAmount - stateFee - ccFee - localRemoteAppFee) * 100) / 100;
}

var rList = new Array();
rList[0] = paymentAmount;
rList[1] = ccFee;
rList[2] = pay4Doc;

console.log(rList[0] + ", " + rList[1] + ", " + rList[2]);

return rList;

Unfortunately, Iā€™m getting ā€œinvalid return statementā€ in the logs.
image

Hereā€™s the editor action window:

@john30 you just need to remove the ā€œreturnā€. From the server script the return statement is the last statement you type.

So just type rList; instead of return rList;

1 Like

It worked! Now I need to access the data that is returned:
image

I donā€™t know how to get to the array that I created though as this isnā€™t working.

@john30 that should be working based on what youā€™ve posted. The only piece that might be throwing it off is Bubble is not 0-indexed. Youā€™re results should really be item #1, item #2, item #3.

@bubble.trouble
Still not working:
image

No values are available:
image
It should say paymentamount = ā€¦

Without a return statement telling the software which variable to send back, how does it know? Iā€™m guessing without ā€˜return ā€¦ā€™, we are doing all the work and then no values are being passed back, which is why the resultsList() is empty.

@john30 No. Something else is going on. You donā€™t need the return statement, and the values will be returned. Take a look here: https://double-trouble-bubble.bubbleapps.io/version-test/ex2?debug_mode=true and editor here: Double-trouble-bubble | Bubble Editor

The IF is captured in that first expression - you are then evaluating the truthy & falsey outcomes of that IF via the format as text. Itā€™s a weird syntax because you lead with your test, not with an IF.

So to summarise, you can use any expression that resolves to true / false, followed by :formatted as text as a Bubble equivalent of IF

1 Like

Hi @bubble.trouble ! First, I want to thank you for helping me figure this out. I see it works in your example. The only difference I see in your code versus mine is that you pass your data to a state and I, being a backend workflow, pass my data to a datatype. Iā€™m still unable to see any data coming out of the script though.

Are you available to connect remotely with me? I can pay you. If so, send me an email and weā€™ll use Teams or Zoom to connect.

You need to access your key:pair input values from the properties.keyvalues object - i.e. appType should be properties.keyvalues.appType etc.

EDIT
Sorry thatā€™s not quite right - properties.keyvalues is an array.
You will need this @john30 :

var outputArr = [];
var index = properties.keyvalues.forEach(function(item){
outputArr.push(item.value);
});
outputArr;
1 Like

@exception-rambler I wish I could find the documentation on all of the syntax needed for using this. Iā€™ve replaced the script with only creating an array and sending it back and that works, so I now know that thereā€™s a problem with my script.

When I run my script outside of bubble, it works, so the problem is definitely, now, where youā€™re helping me: getting the keyvalue pair data. Hereā€™s my updated script that is still not returning the data:

var stateFee = 0;
var filingFee = 0;
var paymentAmount = 0;
var ccFee = 0;
var pay4Doc = 0;

if (properties.keyvalues.insType == ""){
    stateFee = properties.keyvalues.stateFullFiling;
}else{stateFee = properties.keyvalues.stateDiscFiling;}

if (properties.keyvalues.appType == "Marketplace"){
    filingFee = properties.keyvalues.marketplaceBaseFee;}
else{ filingFee = properties.keyvalues.remoteBaseFee;}

paymentAmount = stateFee + filingFee;
ccFee = Math.round(((paymentAmount * 0.029 + 0.30)) * 100) / 100;

if (properties.keyvalues.appType == "Marketplace"){
    pay4Doc = properties.keyvalues.docMarketPay;}
else{
    pay4Doc = Math.round((paymentAmount - stateFee - ccFee - properties.keyvalues.localRemoteAppFee) * 100) / 100;
}


var rList = [];
rList[0] = paymentAmount;
rList[1] = ccFee;
rList[2] = pay4Doc;

console.log(rList[0] + ", " + rList[1] + ", " + rList[2]);

rList;

It doesnā€™t appear that using properties.keyvalues.{kvName} is working. What else could I try to get these?

I posted an edit above - that should sort you out.
Or, revert to vanilla Bubble which is always better where possible. Living across two worlds (which I do with my platform) is the headache you donā€™t need unless thereā€™s no alternative.

1 Like