Trigger Query from SelectedQuery Variable Name

Is it possible to trigger a query based on a variable value?

// Get the value of the selectedQuery variable
const queryName = SelectedQuery.value;

// Trigger the query based on the value of the selectedQuery variable
queryName.trigger();

The code above does not work.

I have queries that are merged into an array and displayed in a table. I have a column that has a button which when clicked sets the value of SelectedQuery to the corresponding query for that row. I want to be able to click that button and trigger only that query to run.

Thanks for any help!

Hi! So it seems that you are not able to trigger the query to run. Do you have an event handler that would trigger the query to run when the button is clicked? It would also be helpful to take a look at the code in a bit more detail. It seems that running code conditionally could definitely be possible but if you could provide more descriptive details on the chain of events along with the code we could get a better sense of what could be going wrong! :slight_smile:

To explain this better I can only call this API 15 times per minute. I have about 70 queries that run and then transform the data into the format I need. I run the queries 15 at a time using a button for each block of 15. Then this data is all combined and put into a table.

I want to have a column in the table that has an icon or button I can click to trigger the query for that specific row. The query for the first row will always be EventCount0, then EventCount1 and so on.

This is where I run into the problem. If I try to set an event handler for the column it will only run that one selected query no matter which row is clicked. I have also tried to use the event handler to set a variable {{SelectedQuery}} and then run script to trigger the SelectedQuery but you can't reference the variable in the script with {{}}. Is there any


way to do this?

Hi @KaiserG13, happy to help! :slightly_smiling_face:

The problem with this approach is that when we reference the value of the "Query" column for any of those rows, it will always be a string (eg. "EventCount0," "EventCount25") and there is no such property "trigger" for a string: "stringExample".trigger().

Depending on how similar those queries are, we should be able to run one query, with different scope. We can achieve this with Additional Scope. For example:

Here is a REST API query that makes a GET request but needs additional data, the ID of an entity (using "httpbin" to keep it simple).


Note: When we use additional scope, the IDE will show red for the values and that is ok. They will be assigned when the query is triggered, this is why the IDE says they are undefined.

On the table, we have a button to run the query, but similar to your example, the query is different for each row as we'll need the id of the specific row:

For that column, we can add an event handler to run the query passing an "additionalScope" object, where we define the key-value pairs we would like to pass as an additional scope.

Note: the value of this key id will be the value for the myId key we set up as a URL parameter on the query.

Here is the query in action:


Note: We can see the correct id was passed in to the query as an additional scope.

Here is a great topic that goes over additional scope in detail.

I'd suggest going with @Paulo 's solution, but if you just want it to work without changing things you can use a massive switch statement (or a bunch of if/else-if statements):

const queryName = SelectedQuery.value;

switch(queryName){
  case 'query1':
    await query1.trigger();
    break;
  case 'query2':
    await query2.trigger();
    break;
  case 'query3':
    await query3.trigger();
    // break;   // if every time you trigger query3 you need to also trigger query4, but you can also trigger query4 on it's own separately you can omit the `break`.  
    //         // when this case 3 runs, it keeps going until it hits a `break` ignoring any case statements along the way.
  case 'query4:
    await query4.trigger();
    break;
  default:
    console.log("oopsie, this query name caused an uh-oh: " + queryName);
}

if you don't need the results from any of the querys in this one resource you can remove all the await's... but if like 20 lines after this you try to use the returned result from the query you'll run into some problems caused by .trigger() returning a Promise and not the expected object/value.

1 Like