Send data from one JavaScript Query to another

So I understand that I can call/run/tigger one JavaScript Query from another using trigger(). Is is possible to also send the called query values as well using additionalScope? If yes, how are those parameters received by JavaScript query being called?

1 Like

@brettski Hi Brett! That is exactly what the additionalScope is used for! In the example here, you can see that additionalScope is an object inside of the .trigger() function. The query that is triggered will receive all of the values listed inside of additionalScope and they can be referenced inside of the script

query1.trigger({ additionalScope: { name: 'hi', }, // You can use the argument to get the data with the onSuccess function onSuccess: function(data) { console.log('Successully ran!'); } })

So there, when query1 runs you could reference ā€œnameā€ anywhere and it would evaluate as ā€˜hiā€™. That works for both JS and other kinds of queries

@alex-w Thanks for the reply. What I donā€™t understand is how do you reference those passed in values in the receiving query? In your example, how do you reference name within query1 (a JavaScript query)?

@brettski ā€“ since query1 is already a JS Query, you should be able to refer to name as name without the {{ }}.

Are there certain variable names we are allowed to use to do this?
I noticed that using name doesnā€™t cause a syntax error. But if I try to use partnerId then the script isnā€™t happy and complains that partnerId isnā€™t defined.
Based on what I am seeing we get one variable, name, which we can use to pass to another query. This is fine, I mean I can pass anything in that variable, I just want to understand the guidelines.

I donā€™t believe that partnerId is a reserved variable name, but maybe you are using it elsewhere in your app as a component or query name and the JS query is confused about what you are referring to?

What I am seeing is that if I put any variable in a JavaScript query which hasnā€™t been declared throws a syntax error. All I have tried except name. Sorry, still confused on this functionality.

@brettski I donā€™t think it should need to be defined/declared before the .trigger function. Could you copy in an example of something that is throwing an error?

Not the calling query, in the query being called. The object used in trigger function is quite clear. It is reading these passed in values in the receiving query which is fuzzy and not understood.

@brettski Hereā€™s a quick example:

The first image is before triggering query1, the second after triggering

Inside the receiving query the variable will be undefined because it IS undefined within the scope of that query. If you trigger query2 directly it would return undefined. When it is run after query 1, it starts off as if var randomkey = ā€œrandomValueā€ was added before the rest of the code. You can also use other kinds of queries and use {{randomKey}} inside of the query builders for resources.

Yeah, messy, undeclared variable error raised, but okay.
So I just came back to this and thought, well why donā€™t I declare it. I assume it will be set when called and it seemed to work that way.
so, in your example
`var randomeKey;

text1.setValue(randomKey); // cuz all keys should be exposed! :)`
Sorry for the long threads, I am not sure why I didnā€™t think of declaring the variable at the start. Do you know of any issues with this approach I may run into?

I usually do something like:
if(!typeof randomKey){ var randomKey = value I want if It isn't passed in additionalScope }

We donā€™t want to overwrite the value that is passed if the variable is already defined before the script starts

Where did the images go :cry: ? I was trying to do this again and am running into trouble. The same trouble, LOL

Hey @brettski!

Happy to help here! This is a screenshot of the triggered query, query1 if no value is passed in via additionalScope


If triggered by another query and additional scope is passed in like this:

Then query1 returns the passed value:

triggered_query copyable code here:

query1.trigger({additionalScope: {
randomKey: "passed value"
}});
query1 copyable code here:
if (typeof randomKey === 'undefined') { var randomKey = "value I want if It isn't passed in additionalScope" }

return randomKey;
Hope this helps, let me know if you have any questions! :slight_smile:

Yeah, it was that first one I was needing. Thank you!

Just in case images vanish again when I am looking for this two year from now again :slight_smile:

if (typeof randomKey === 'undefined') { var randomKey = 'value I want if no passed in additionalScope');

return randomKey;
1 Like