Changing Global Variable in JS code block doesn't work


I am attempting to change a variable I created in a block using JS code. Unfortunately, the changes made in the code do not reflect in the variable after running the program. The code has been verified in VS Code to run properly and to the spec as needed.

Additionally, the global variable declaration and subsequent code is very similar to behavior in another bot that DOES work.

Finally, by adding a bit to the code that throws an error if the variable is properly updated within the context of the code, I am able to verify that until the bot exits the JS Code block, the variable is properly updated, and then it reverts to the initial state as if it were a local variable deleted when it goes out of scope.

Edit: This topic is resolved, turns out throwing an error causes the platform to dump any changes to global variables made in the JS block. This should be described in the documentation for the “Execute JS code” function

Does throwing an error appear before changing the variable in the JS code block?

No, the error throwing line was the last line in the block.

Please, create a minimal example which replicates the issue and send it to us.

Here you go.

Thank you, the behavior is the same. We will take a look at it and return to you.

I’ve just clarified the question within our technical team. It’s not a bug and let me explain why. We are waiting for a block to completely finish its action. If there are no errors during the execution of this block, then the information (hence, variables) are updated. If there is an error then there are no updates and the error message appears.

In your case you can solve it in two different ways.
First of all, you can split one “Execute js code” block to two blocks. The first one will update the variables and the second one will throw an error in case of something.
Or you may use a sort of boolean flags technique and reorganize errors handling approach in your bot. I can’t help you with more details on this method because I need to know the details of your code. But I think if you understand the reason why it happened (explained above) you’ll be able to find a proper solution for that.

Thank you for your speedy response! I’ve worked around the behavior already so this will not be an issue in the future.

You should update the documentation of the Execute js code block, though, to include this information as it was not there when I searched.

Okay, thank you for the feedback. We’ll take it into consideration. - the information is added.