Pulling Field from ReTool DB, Using it in a Script

I'm trying to pull a data item from a ReTool DB and then to assign it to a variable and use it in a script, however I'm not able to get it working properly.

Here is the script I'm attempting to use

let day_stage = await get_day_stage.trigger()
if (day_stage == "Morning") {
    return day_stage
      utils.openUrl("https://exampleredirecturl", { newTab: false})
  }
else {
  return "fail"
}

Here is the SQL query being triggered

SELECT day_stage FROM day_log WHERE date = CURRENT_DATE

I'm a bit confused why this isn't working, as if I just run this script:

let day_stage = await get_day_stage.trigger()
return day_stage

I get the expected output of "Morning":

day_stage: [] 1 item
0: "Morning"

My guess is that day_stage is an array and I'd need to look at the index "0", however numerous tries to do so in the if statement using functions like indexOf or day_stage[0] === "Morning" didn't work, so maybe there's another problem?

Hey there @evansignup, and welcome to the forum!

The issue you're having is that get_day_stage.trigger() returns an object containing metadata and the actual data is accessible via the .data property. Therefore, to obtain the day_stage value, you should reference day_stage.data.

Here's how you can modify your script:

let day_stage = await get_day_stage.trigger();
if (day_stage.data[0] === "Morning") {
  utils.openUrl("https://exampleredirecturl", { newTab: false });
} else {
  return "fail";
}

Let me know if this solves the issue for you!

1 Like

Thanks! That is getting me closer, the function below is outputting correctly when nestled in the if statement-

return day_stage.data[0]

However when I put a redirect in there, and identical one to one I have in a different script (that works), it isn't functioning. It should run as the condition on the if statement is satisfied but whether I tie the script execution to a button or on page load (as desired), nothing happens.

The full statement I'm trying to execute is:

utils.openUrl("https://evaninternal.retool.com/apps/d5222fe8-e870-11ef-88d4-371733ba4d85/Task%20System/morning_design", { newTab: false})

Doesn't make sense to me why this isn't triggering now, but the exact same line will run in other scripts.

Hey @evansignup

Why don't you add an event handler to your get_day_stage query, with an "only run" condition like self.data[0] === "Morning"

image

I think your js query may not be working due to a race condition, but you can implement the above and save yourself a query.

2 Likes

Thanks! Very new to Retool but this seems like a much better way to organize it. Sadly it also isn't working.

SELECT day_stage FROM day_log WHERE date = CURRENT_DATE

Query is set to Automatic, so it should run on page load.

Success handler-
Go to URL
URL - https://evaninternal.retool.com/apps/d5222fe8-e870-11ef-88d4-371733ba4d85/Task%20System/morning_design
Only run when - {{ self.data[0] === "Morning" }}

1 Like

The query is running on page load, it's just not properly triggering the redirect

Try with {{ self.data.day_stage[0] === "Morning" }}

2 Likes

That did it, thanks!

1 Like