Can I save the results of a query in another GlideRecord variable and then perform queries on that new GR?

BP6
Tera Contributor

Let's say I create a new GR and then perform a query on it like so:

var gr1 = new GlideRecord('table');
gr1.addQuery('x', y);
gr1.query();

I want to then store the results of this query in a new object, and then perform more queries on that object instead of querying gr1 again. I want to do this because I want to keep the results in gr1 intact, and then use other objects to further filter down the results if possible. 

How can I do this?

1 ACCEPTED SOLUTION

SumanthDosapati
Mega Sage
Mega Sage

Hi,

You can try this way

var records = "";
var gr1 = new GlideRecord('table');
gr1.addQuery('x', y);
gr1.query();
while(gr1.next())
{
records = records + gr1.sys_id + ",";
}
var encodedquery = "sys_idIN"+records;

//now you can use the above encoded query in other gliderecords
//for example
var gr2 = new GlideRecord('table');
gr2.addEncodedQuery(encodedquery);
gr2.query();

 

Mark as correct and helpful if it solved your query.

Regards,
Sumanth

View solution in original post

3 REPLIES 3

Uncle Rob
Kilo Patron

gr1.query() is what finally goes to the database and does the thing.

You won't be able to avoid a second query()

SumanthDosapati
Mega Sage
Mega Sage

Hi,

You can try this way

var records = "";
var gr1 = new GlideRecord('table');
gr1.addQuery('x', y);
gr1.query();
while(gr1.next())
{
records = records + gr1.sys_id + ",";
}
var encodedquery = "sys_idIN"+records;

//now you can use the above encoded query in other gliderecords
//for example
var gr2 = new GlideRecord('table');
gr2.addEncodedQuery(encodedquery);
gr2.query();

 

Mark as correct and helpful if it solved your query.

Regards,
Sumanth

Thank you! I like your approach and it seems to do what I need