- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2023 12:36 AM
Hi,
I written a script to push ci's to custom table in that I'm showing related ci's having having record count.
I want push number count into "u_item_count" field
Script:
var RowCount = 0;
var gr = new GlideAggregate("OOB TABLE");
gr.addEncodedQuery("active=true^cmdb_ci.sys_class_name=cmdb_ci_server^ORcmdb_ci.sys_class_name=cmdb_ci_endpoint^ORcmdb_ci.sys_class_name=cmdb_ci_computer");
gr.addAggregate('COUNT', 'cmdb_ci');
gr.orderByAggregate('count', 'cmdb_ci');
gr.setLimit(10);
gr.query();
while(gr.next()){
alertRowCount = gr.getAggregate('COUNT', 'cmdb_ci');
gs.log('count'+alertRowCount );
var topItem = new GlideRecord("Custom table");
topItem.addQuery("u_configuration_item", gr.getValue("cmdb_ci"));// pushing into custom ci field
topItem.addQuery("u_item_count", gr.getValue("COUNT"));
topItem.query();
if (!topItem.next()) {
topItem.initialize();
//topItem.setValue("u_vulnerable_item_count", gr.getValue("COUNT"));
topItem.setValue(("u_item_count", gr.getValue("COUNT"));
topItem.setValue("u_configuration_item", gr.getValue("cmdb_ci"));
}
var sysId = topItem.update();
//records.push(sysId);
}
Can anyone please help me on this.
Regards,
Sanju.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2023 01:48 AM - edited 09-04-2023 01:50 AM
Hi @Talari Balateja Can you please try with the below script
var RowCount = 0;
var gr = new GlideAggregate("OOB TABLE");
gr.addEncodedQuery("active=true^cmdb_ci.sys_class_name=cmdb_ci_server^ORcmdb_ci.sys_class_name=cmdb_ci_endpoint^ORcmdb_ci.sys_class_name=cmdb_ci_computer");
gr.addAggregate('COUNT', 'cmdb_ci');
gr.orderByAggregate('count', 'cmdb_ci');
gr.setLimit(10);
gr.query();
while(gr.next()){
alertRowCount = gr.getAggregate('COUNT', 'cmdb_ci');
gs.log('count'+alertRowCount );
var topItem = new GlideRecord("Custom table");
topItem.addQuery("u_configuration_item", gr.getValue("cmdb_ci"));// pushing into custom ci field
topItem.addQuery("u_item_count", gr.getValue("COUNT"));
topItem.query();
if (!topItem.next()) {
topItem.initialize();
//topItem.setValue("u_vulnerable_item_count", gr.getValue("COUNT"));
topItem.setValue(("u_item_count", alertRowCount);
topItem.setValue("u_configuration_item", gr.getValue("cmdb_ci"));
}
var sysId = topItem.update();
//records.push(sysId);
}
Thanks & Regards,
Eswar Chappa
Mark my answer correct and Helpful if this helps you 😀
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2023 12:48 AM
you already have the count right in the variable alertRowCount
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2023 01:31 AM
Hi Ankur,
I have the count but I'm not able to push the count to field.
Regards,
Sanju.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2023 01:48 AM - edited 09-04-2023 01:50 AM
Hi @Talari Balateja Can you please try with the below script
var RowCount = 0;
var gr = new GlideAggregate("OOB TABLE");
gr.addEncodedQuery("active=true^cmdb_ci.sys_class_name=cmdb_ci_server^ORcmdb_ci.sys_class_name=cmdb_ci_endpoint^ORcmdb_ci.sys_class_name=cmdb_ci_computer");
gr.addAggregate('COUNT', 'cmdb_ci');
gr.orderByAggregate('count', 'cmdb_ci');
gr.setLimit(10);
gr.query();
while(gr.next()){
alertRowCount = gr.getAggregate('COUNT', 'cmdb_ci');
gs.log('count'+alertRowCount );
var topItem = new GlideRecord("Custom table");
topItem.addQuery("u_configuration_item", gr.getValue("cmdb_ci"));// pushing into custom ci field
topItem.addQuery("u_item_count", gr.getValue("COUNT"));
topItem.query();
if (!topItem.next()) {
topItem.initialize();
//topItem.setValue("u_vulnerable_item_count", gr.getValue("COUNT"));
topItem.setValue(("u_item_count", alertRowCount);
topItem.setValue("u_configuration_item", gr.getValue("cmdb_ci"));
}
var sysId = topItem.update();
//records.push(sysId);
}
Thanks & Regards,
Eswar Chappa
Mark my answer correct and Helpful if this helps you 😀