How to get data from a query to then dynamically build url for second query? And also send some of that data as payload in second query?

Hi there. I'm quite new to retool. Have only been using it a week and have set up basic queries from forms. I am unable to figure out so far how to do more a complex operation mentioned below.

I have a form that sends query1 on clicking submit button.

  • If query1 is successful, I will get back an "id" value in the response data.
  • I then want to build and trigger query2 which will need a dynamic url. The url will need the "id" as a part of the it.
  • Also, query2's will have json data to be sent, which will also need to have the "id" value in it, along with a another value. i.e. the json payload sent for query2 should look something like this { "key1":"id", "key2":"other input value in original form"}

I am not really able to understand how to create queries that send their response data to be used in a second query. Transformers are read only. Query triggers can't send the url as well can they?

Hey @Irfan, welcome to retool :))

Looks like you need a JS query that takes care of:

  • Sending the data from the form (query1)
  • Taking the response of query1, transforming the data and sending it with query2
const response = await new Promise((resolve) => {
  query1.trigger({
    additionalScope: {
      formInput1 : formInputValue1,
      formInput2: formInputValue2
    },
    onSuccess: (data) => {
      resolve(data);
    },
  });
});

query2.trigger({
  additionalScope: {
    id: reponse.data.id // you'll need to fix this to access the values you want
  }
})

You could probably build in some error handling etc but that's the barebone.

In your queries you can access additionalScope variables by enclosing them in curly brackets. For example, query1 would need to have formInput1 and formInput2 variables to interpolate.

Try that :))

1 Like