How to force-trigger a query from a transformer?

I have a button that on click runs a postgres query (Q1), which references a transformer (T1), which references another postgres query (Q2).

Button => Q1 => T1 => Q2

When I first load the page and click on the button, everything works.

But if I click the button again, Q2 doesn’t run again. It simply gives me the data it got from the previous run (even though neither query is set to cache their results).

Is there a way to force trigger Q2 every time I run Q1?

1 Like

Hey @nacho, hope all is well :slight_smile:

You can trigger any query with query.trigger(), and even pass along additional scope: https://docs.retool.com/docs/scripting-retool

Hi Justin,

The problem is that in my transformer (T1), I currently have: const q2 = {{ q2.data }}; but I can't do: const q2 = {{ q2.trigger().data }};.

The only solution I see (and what I was trying to avoid) is to set the button to a new JS query that triggers Q2 and then (if successful) calls Q1.

I have so many queries in the panel that it's getting hard to keep track of them. :sweat_smile:

@nacho you wouldn’t reference the query’s data from the trigger - it would be more like

q2.trigger()
const q2 = q2.data

The trigger ensures there will be data in q2.data.

Thanks, but I can't do that in a transformer, can I? It appears in red:

image

Ah good point. Can you set up q2 to run on the Postgres query’s success? Or move the transformer code into a Run JS Code query?

Q1 doesn't run independently from Q2, it references T1, which references Q2.

Q1: insert ... values {{ t1.value }}
T1: const data = {{ q2.data }}

So, I guess the only way to ensure that q2 gets triggered is by changing T1 into a JS query and doing q2.trigger() there.

EDIT: Actually, I can't get this to work either. I would need to make these changes, right?

Q1: insert ... values {{ q3.data }}
Q3:

let data = await q2.trigger(); // this is actually inside an async function
// manipulate data
return data;

But I'm getting an error on Q1: syntax error at or near "null"

I think I need a dependency graph here :crystal_ball: I'm guessing this error has to do with let / await. The .trigger() method has an onSuccess param. So you could do:

q2.trigger({
    onSuccess: function(data) {
        return data;
    }
})

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

This is the dependency graph :sweat_smile:

The problem is that the trigger returns a promise that resolves to the data. But I don't want to return Q2's data, I want to return a modified version (which is what I do in T1).

The issue with the graph is that it seems like you need to trigger Q1 and Q2 for T1 to do its work. I’m having a hard time visualizing this - any chance you can write into support?

You mean on Intercom? Ok. I'll do that. :+1:

1 Like