
- 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-19-2019 06:56 AM
can you try and let me know the log details
var rel2 = new GlideRecord('cmdb_rel_ci');
rel2.addEncodedQuery("type=55c95bf6c0a8010e0118ec7056ebc54d^child.sys_class_name=cmdb_ci_db_mssql_instance^parent.sys_class_name=cmdb_ci_database^parent.sys_id="+db+"");//db variable holds one database record
rel2.query();
while(rel2.next()){
var instances = rel2.child;
var instCount = rel2.getRowCount();//this is giving me the correct count in this case 3 count
var arr = instances.split(',');
for(var i = 0;i<arr.length;i++){
var inst = new GlideRecord('cmdb_ci_db_mssql_instance');
inst.addQuery('sys_id',arr[i]);
inst.addQuery('install_status', '7');
inst.query();
while(inst.next()){
gs.log('Count is:'+inst.getRowCount());//always gives me 1 as count
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2019 07:03 AM
Thanks for your effort on this. I received the below output:
*** Script: Count is:1
*** Script: Count is:1

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2019 07:09 AM
lets troubleshoot the above script
put the log after the for loop to check how many sysid is coming in arr[i]
gs.log('length is :'+arr[i]);
once you get that then run your 2nd glide record in another background script to validate how many number of rows it return this way we can identify the issue in the script .

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2019 07:17 AM
Here is the outputs for different logs:
gs.log('length is :'+arr[i]);
Gave me 4 records since DB has 4 instances.
while(inst.next()){
gs.log('length is :'+arr[i]);
Gave me 2 records since 2 instances are retired.
gs.log('Count is:'+inst.getRowCount())
Gave me below output
*** Script: Count is:1
*** Script: Count is:1

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2019 07:10 AM
Harshavardhan,
The whole idea here is to find out if all record that are queried through instance table are retired. For example, if I query 5 records, if loop should be true when all records are retired. If one of them is not retired then loop should give false.