All instructions in a condition are executed

I am looking to manage the status of buttons that are used to filter data. For that, I have a json configuration file: each time a button is clicked, a condition looks if this filter is already present in the config file. If it is, it removes it, if it is not, it adds it. So I wrote a javascript like this (simplified for a better understanding):
if(contains(config[filter],context.item[‘filter’]),remove(config[filter],context.item[‘name’],add(config[filter],context.item[‘name’])).

But this does not work normally: both operations (add and remove) are performed systematically. I can see this simply by changing the “remove” to an “add”, in which case the filter is added twice in the config file.
It is as if there were no condition, but simply two lines of instructions that are executed.
I even tried to modify the condition such as: if(true==false,…). Even in this case, the two instructions in the condition are executed!

Do you have the JavaScript in a workflow?

Can you paste the whole code.

You’re missing a parenthesis on the example for remove. )

Yes, javascript is in a “click” workflow.
Here a video with the bug :
https://www.transfernow.net/dl/20220715ZANy7LQF
Here the code :

wwFormulas.if(wwFormulas.contains(variables['a57c1775-0fec-4080-9b46-5a21c8ac4c3d'][context.item['parent']['data']['category']],context.item.data['name_en']),
    variables['a57c1775-0fec-4080-9b46-5a21c8ac4c3d'][context.item['parent']['data']['category']] = wwFormulas.add(variables['a57c1775-0fec-4080-9b46-5a21c8ac4c3d'][context.item['parent']['data']['category']], context.item.data['name_en']), variables['a57c1775-0fec-4080-9b46-5a21c8ac4c3d'][context.item['parent']['data']['category']] = wwFormulas.add(variables['a57c1775-0fec-4080-9b46-5a21c8ac4c3d'][context.item['parent']['data']['category']], context.item.data['name_en']));
context.item.data['active'] = !context.item.data['active']

wwFormulas.if is a function not a if else statement. Assignments or other functions in the parameters are all executed.

You should use an if else statement.

1 Like

Thanks dorilama ! I thinked wwFormulas.if is same than pure javascript if/else. Then i don’t understand how wwFormulas.if works and what is its utility, but from now, i will use pure javascript when it’s needed.

1 Like

the utility is if you want to choose between two values with the nocode formula.
The equivalent of if else in nocode workflows will be the new branching feature.

1 Like

The noCode if formula is good to use, but you want to use it different than you were.

The wwFormulas if is going to return a value. So, if you want to change a value of a variable in the workflow, for example, it would be good to use the noCode formula.

Or if you want to conditionally display an element … Bind the elements display property and use noCode if formulas like if(contains(roles_array,“Admin”)), true, false)

The above would return true if the roles_array contains “Admin”

1 Like