
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2019 01:38 PM
Hello,
I am trying to get the count of all record that matches the query condition. When I use the below script, the count is always being '1' even though there are 1+ records that matches the condition. I am I missing anything? Please suggest.
var count = 0;
var gr= new GlideAggregate('cmdb_ci_db_mssql_instance');
gr.addAggregate('COUNT');
gr.addQuery('sys_id', 'IN', instances);//This variable holds 1 or more record sys ids
gr.addQuery('install_status',7);
gr.query();
while(gr.next()){
instanceCount = gr.getAggregate('COUNT');
gs.print(instanceCount);
}
This time I have 2 records that matched the condition
Output:
*** Script: 0
*** Script: 1
*** Script: 1
*** Script: 0
Expected Ouput: 2
My expectation is that these records are queried one after one that's the reason I am getting individual record's count. Is there any way to count all looped records and combine and output as 2? Thanks
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2019 09:17 AM
here we go
var count=0;
var rcount;
var gr = new GlideRecord('cmdb_rel_ci');
gr.addQuery('child.sys_id','d53a14fddbd8330080e0a08a48961902');
gr.query();
if(gr.next())
{
gs.print(gr.parent.getDisplayValue());
var gr2 = new GlideRecord('cmdb_rel_ci');
gr2.addQuery('parent.sys_id',gr.parent);
gr2.query();
rcount=gr2.getRowCount();
while(gr2.next())
{
if(gr2.child.install_status== 7){
count++;
//gs.print(gr2.child.install_status);
}
}
}
if(rcount == count)
{
gs.print('hey harsh please update the database');
}
else
{
gs.print('no need to update the database');
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2019 06:23 AM
I am at client site, can't have external calls. Please give me your email I can replicate the scenario in my PDA and get you the access.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-20-2019 06:25 AM
hvrdhn88@gmail.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2019 02:30 PM
Hi Rick,
how do you have defined "instances" variable? Is it comma separated string or array?
It should be something like:
'd522997fdbc8fb0073b9d7b7f4961956,e142f07fdb48fb0073b9d7b7f4961997,598299e3dbc4bb0073b9d7b7f496198f'

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2019 02:38 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2019 03:33 PM
From my point of view your script looks fine, however I'm unsure about the "instances" variable.
Can you make it a comma separated string?
Also assignment like:
var instance = gr.sys_id;
is not really correct if you are doing it in loop. You should do instead:
var instance = gr.getUniqueValue();
or
var instance = gr.getValue('sys_id');
--------------
so if you are doing something like
var instances = [];
... query
while(gr.next()){
instances.push(gr.getUniqueValue());
}
....
....
then use in your script like:
....
gr.addQuery('sys_id', 'IN', instances.join(','));