Use the results of a GlideRecord in another GlideRecord query

Chris17
Tera Contributor

Hello,

Currently I have a requirement to get active items from one table, store them in an array, and then used the results of that array as a query in another GlideRecord. Currently I have the two separate glide records, but I am unsure of how to combine them.

	var managed_cis = [];
	var gr = new GlideRecord("x_qune_pa_managed_ci_class");
	gr.addQuery('active', 'true');
	gr.query();
	while(gr.next()){
		managed_cis.push(g.getValue('ci_class'));
	}
 var cmdb_dyn = new GlideRecord("cmdb_ci");
    cmdb_dyn.addQuery('name', 'DOES NOT CONTAIN', 'vmware');

    cmdb_dyn.query();

How do I use the results of the first query and filter the second one based on them?

1 ACCEPTED SOLUTION

Aoife
Tera Guru
var managed_cis = [];
var gr = new GlideRecord('x_qune_managed_ci_class');
gr.addActiveQuery(); // same as gr.addQuery('active', 'true');
gr.query();
while (gr.next()) {
   managed_cis.push(gr.getValue('name'));
}

var cmdb_dyn = new GlideRecord('cmdb_ci');
cmdb_dyn.addQuery('name', 'IN', managed_cis.join(','));
cmdb_dyn.query();

However, if you know the class name for the x_qune_managed_ci_class table (I assume it extends a cmdb_ci table), you can do it in one query by just addQuery ('sys_class_name', '{quneclassname}');

Aoife

View solution in original post

2 REPLIES 2

Aoife
Tera Guru
var managed_cis = [];
var gr = new GlideRecord('x_qune_managed_ci_class');
gr.addActiveQuery(); // same as gr.addQuery('active', 'true');
gr.query();
while (gr.next()) {
   managed_cis.push(gr.getValue('name'));
}

var cmdb_dyn = new GlideRecord('cmdb_ci');
cmdb_dyn.addQuery('name', 'IN', managed_cis.join(','));
cmdb_dyn.query();

However, if you know the class name for the x_qune_managed_ci_class table (I assume it extends a cmdb_ci table), you can do it in one query by just addQuery ('sys_class_name', '{quneclassname}');

Aoife

OlaN
Giga Sage
Giga Sage

Hi,

 There is a small typo in your scripts, in the while loop.

//managed_cis.push(g.getValue('name'));
managed_cis.push(gr.getValue('name'));