HELP - Query Returning Old Results

I'm working on a Retool app that needs to run a Postgres query whenever a user enters a product code (scanCode). However, I'm consistently seeing old results from the previous search instead of the current one. Sometimes on the second or third try, it finally shows the correct data, but then stays one step behind again for subsequent searches.

Details so far:

  • I’ve tried adding a dynamic “forceRefresh” parameter (using something like moment().unix()) to avoid caching, but Retool either rejects extra parameters (due to prepared statements) or it still returns the result from the previous code.
  • The query is set to manual trigger, no automatic running on input change, and no “run on page load”.
  • I’ve confirmed the correct values (inputScanCode.value, etc.) via console.log right before calling queryName.trigger(). In the Retool query “Preview” tab, the exact same parameter values return the correct rows, but in the actual app code it shows old or empty data the first time, then the correct data on the next attempt (always one cycle behind).
  • I also tried constructing the entire WHERE clause as a single string (one parameter in the SQL), forcing a brand-new value every time, and adding a small delay (await new Promise(r => setTimeout(r, 200))) before triggering the query—yet it still yields the previous search’s data.
  • There’s no visible option to disable prepared statements in my Retool environment, so each {{ }} reference is treated as a separate parameter. I’ve had to consolidate them, but the old-results issue persists.

What I Need:

  • A reliable way to ensure that each query execution uses the current parameters (especially the product code) and does not reuse or return old results.
  • Any steps or best practices to eliminate caching or delayed state updates in this scenario.

Hello @agustinsaezm and welcome to the forums!

Would you be able to share the setup of your queries and their Advanced tabs? In each query there is an option for caching results that may have been enabled.

Also, if you are looking for a way to execute a statement without using the setting in the resource to disable converting to a prepared statement you can create a JS Query to "prepare" your statements and then EXEC the returned string (Dynamically generated SQL query in Retool - #2 by pyrrho). This will also let you check on the values being passed directly to the query.

ahhhhh so thats how you get around the prepared statement setting! thx!

... which is there for good reason! I do encourage anyone using this method to ensure that options are rigidly defined (or otherwise sanitized in the JS Query) so that mischievous beings aren't tempted to try and Bobby Tables you.

1 Like

Thanks Pyrrho, I wasn't able to solve it this way, but I figured it out another workaround :slight_smile: thanks!

1 Like

I'm glad to hear it! Out of curiosity, what was the underlying issue and how did you get around it?

Hi Darren, not proud about it. In the script of my the button I created a logic that forces the user to click on it 2 times... the first time just trigger the query with the values of the text input... the second time asign my variables the results of the query. Never understood why it didn't work on just 1 script (trigger query + asigning results to variables)... and as I'm not a developer, I wasnt able to understand and apply the solution provided by Pyrrho.

Not a big deal if it works! If you are interested in additional debugging, though, I'd be happy to take a look.

That would be great... maybe if I record a video of the issue and send you my current script?

Seeing the script that you're running is probably sufficient! Based off of what you're described so far, though, my guess is that you can fix the issue by toggling on the following option in the script's Advanced tab.

Hello Darren, that would make sense, but I've never seen that option.. can you guide me where it is?

Would he need to await the calls to setValue also or is setIn the only one that returns a promise?

the docs don't specify the return type and the examples use await for setIn but not for setValue

1 Like

Ah you'll need to move your script into a dedicated JS query. The setting you're looking for is in the Advanced tab.

To @bobthebear's point, it may also be necessary to await your setValue expressions.