I have the following Custom Javascript in a workflow ation to add an item to an array variable.
variables[/ VariableName / ‘123456789’].push({foo:“bar”})
When I run the code in the page, or with the test action link, it automatically sets the Default Value of the array variable as well as the value.
Why does the code change the default value. How do I avoid this?
2 Likes
Hi Kevin,
this is because you are updating the array itself instead of setting the value to a new array.
In no code/formula we are taking care for you to treat variable as immutable. If you are going to the js way, we need to be carefull about it
variables['123'] = [...variables['123'], {foo: "bar"}]
If you are not familiar with this synthax, we are just creating a new array and assign it to the var. When you do that, magic of Weweb can happen (always assign directly the value to the var)
This is also true for object, never update a subkey of a var, always create a new object.
If you want you can also use our safe formula method, even in JS
variables['123'] = wwFunctions.add(variables['123'], {foo: "bar"})
Hope it helps, let me know if you need more information
3 Likes
You can even use only nocode formulas, like this
2 Likes
Awesome, Thank you! That makes sense and will take some getting used to remembering.
2 Likes