Share feedback, ideas and get community help

Updated 4 months ago

Help with script in typebot

I have a problem with scripts in typebot, basically i have a variable equal to 2 and i made a script to return variable-1, after the script runs i puted a block with que variable and the value is the same as before

The context is i have 15 variables and i ask to the user to write a number that needs to be the same as the variable, each variable have a quantity, after this i put a js block with this code
switch({{codigoaremover}}) {
case 1:
return {{resultadoqtd1}} = {{resultadoqtd1}} - {{Quantidade a remover}};
case 2:
return {{resultadoqtd2}} = {{resultadoqtd2}} - {{Quantidade a remover}};
case 3:
return {{resultadoqtd3}} = {{resultadoqtd3}} - {{Quantidade a remover}};
case 4:
return {{resultadoqtd4}} = {{resultadoqtd4}} - {{Quantidade a remover}};
case 5:
return {{resultadoqtd5}} = {{resultadoqtd5}} - {{Quantidade a remover}};
case 6:
return {{resultadoqtd6}} = {{resultadoqtd6}} - {{Quantidade a remover}};
case 7:
return {{resultadoqtd7}} = {{resultadoqtd7}} - {{Quantidade a remover}};
case 8:
return {{resultadoqtd8}} = {{resultadoqtd8}} - {{Quantidade a remover}};
case 9:
return {{resultadoqtd9}} = {{resultadoqtd9}} - {{Quantidade a remover}};
case 10:
return {{resultadoqtd10}} = {{resultadoqtd10}} - {{Quantidade a remover}};
case 11:
return {{resultadoqtd11}} = {{resultadoqtd11}} - {{Quantidade a remover}};
case 12:
return {{resultadoqtd12}} = {{resultadoqtd12}} - {{Quantidade a remover}};
case 13:
return {{resultadoqtd13}} = {{resultadoqtd13}} - {{Quantidade a remover}};
case 14:
return {{resultadoqtd14}} = {{resultadoqtd14}} - {{Quantidade a remover}};
case 15:
return {{resultadoqtd15}} = {{resultadoqtd15}} - {{Quantidade a remover}};
}

The problem is after the script run the new value in the variable is not actualized
J
g
15 comments
Uh, for me usually I can only set the variable from javascript with a return value at the end.

Could you just make this into a function and then solve which value satisfies the condition?

The return at the end for me works without a ;

So

return myFunction(paramerters)

But I'm not sure. I can't even understand what you are wanting from this- an array or one result..
But I guess if you are trying to use a switch statement and then testing which case returns the value in the parameter, are you expecting it to the case such as "case 4" or "case 12"? Sorry I'm not the best programmer but what are you trying to return?
I have 15 products in my typebot, each product has a code and in this step of the flow there is an area to remove product quantity, so the user types the example code:
removecode
then I show the item and the quantity that was added, and the user types the quantity to remove and in this code

In theory, if the removecode === productcodeX, the amount that the user typed must be reduced by the amount of the product and this value must be saved in the variable that has the quantity, but the value is not being updated.
For example here :
case 1:
return {{resultadoqtd1}} = {{resultadoqtd1}} - {{Quantidade a remover}};

This variable {{resultadoqtd1}} must update the value to this new value
but that's not happening
i also tried to use setVariable in the place of return
and i got this
ReferenceError: setVariable is not defined
setVariable never worked for me either. I've tried.

Only thing that has worked for me is
___

return myFunction()

Or

return myFuntion(parameters)

But if you are just trying a simple function to update a variable, why don't you just put your resultadoqtd variables into an array instead of separate variables. The array woud look like this (and you could call it resultadoqtdArray):

["resultadoqtd1", "resultadoqtd2", "resultadoqtd3", "resultadoqtd4", "resultadoqtd5", "resultadoqtd6", "resultadoqtd7", "resultadoqtd8", "resultadoqtd9", "resultadoqtd10", "resultadoqtd11", "resultadoqtd12", "resultadoqtd13", "resultadoqtd14", "resultadoqtd15"]


You can build an array programatically using a loop and the "append" option in the set variable block.

And then your variables are now:

{{={{resultadoqtdArray}}[{{index}}]=}} or {{={{resultadoqtdArray}}.at){{index}})=}}

I prefer the first way- examples from typebot documentation here:

Variables in Typebot

Then, all that you need to do is set your index based on which value in the array you want modified (assuming you are storing that in {{codigoaremover}} - which I would write as {{codigoA_Remover}} or something more obvious.

just corelate that to the index somehow so you are getting the correct value and remember arrays start at 0 so {{resultadoqtd1}} would now be

{{={{resultadoqtdArray}}[0]=}}

So you basically have to specify which product in the array you are going to change the value for and set that to your index variable. then your code block is just:

{{={{resultadoqtdArray}}[{{index}}]=}} = {{={{resultadoqtdArray}}[{{index}}]=}} - {{Quantidade a remover}}

That is all you need

Should work fine but I am curious if there is a way to make that setVariable funtion work to set typebot variables through javascript if @baptiste weighs in.
Oh I just realized- you will need to have a variable in your setVariable block which I would set to something like tempResultado

Then in the block it is:

{{={{resultadoqtdArray}}[{{index}}]=}} - {{Quantidade a remover}}

And you would need to use some javascript in the block to basically rewrite the array with the new value. hold on---- (leaving this part in here so you can see my thought process but this is NOT the way to do it)
this is the way:

Set your setVariable block to:

resultadoqtdArray

Then in the block:

function updateArrayValue(array, index, valueToRemove) {
// Subtract the valueToRemove from the element at the given index
array[index] = array[index] - valueToRemove;

// Return the updated array
return array;
}

// This line returns the updated array in Typebot's context
return updateArrayValue({{resultadoqtdArray}}, {{index}}, {{Quantidade a remover}});


---- I am actually going to be trying this in something I'm doing so this process turned out to be helpful for me too. I am not a coder so I use ChatGPT for help a lot- here is my dialogue for this so you can see how I use it to help me with code:

https://chatgpt.com/share/3235ea9c-ec9e-424d-b6ff-40adab0f0a96
You do need a variable for your Quantidade a remover though- so like quantidadeToRemove
Here you go:

function updateArrayValue(array, index, valueToRemove) {
// Subtract the valueToRemove from the element at the given index
array[index] = array[index] - valueToRemove;

// Return the updated array
return array;
}

// Return the updated array back to Typebot
return updateArrayValue({{resultadoqtdArray}}, {{index}}, {{quantidadeToRemove}})
You could use ARemover also but I just don't like one letter words in variable names with camel case just a personal preference use what you want.
oh shoot and remember, in my experience at least, you never want a ; at the end of the return line where you call the function and return the result to your variable in the block...
I fixed it but keep an eye out for that.
Add a reply
Sign up and join the conversation on Discord