How do I use temporary state variables in transformers?

Hi,
I'm trying to set the value of a state variable within my transformer but can't quite figure out the syntax. Things I've tried:

  1. {{state1.setValue(3)}}
  2. state1.setValue(3);

Is it only possible to call setValue inside a Run JS query? Thanks!

Hey @amitavk, and welcome to the community! You are correct - transformers are basically read only, so you can’t set the values of other components / state and you can’t trigger other queries. You’ll need to use a JS Code query to do this.

I realize that this isn’t in our documentation, and apologies for that - adding it in right now!

3 Likes

What would the best way to chain requests be then? I have queries A, B, C that I need to run in sequence on success of the prior on submitting a form. How do I use a value from the response of A in the request of C?

Temp variables seemed obvious for this to me. But if there's no place to hold variables in-between a sequence of calls, the only hack I can think of is having an arbitrary text component where I'm setting my temp variable :man_shrugging:

1 Like

@amitavk you can use a JS Code query (just create a new query and select “Run JS Code” from the dropdown). You can trigger a query with query.trigger() and there’s an onSuccess handler that will let you run another query on success (and so on and so forth). You can also pass additional scope from one query to another, which should take care of your “use value from A in request of C” use case.

You also can definitely use temp state if needed. In JS Code queries, you can set the value of an existing temp state variable (via .setValue).

More info here: https://docs.retool.com/docs/scripting-retool

2 Likes