The CreatorCon Call for Content is officially open! Get started here.

Want to show backgroung script count number in field

Talari Balateja
Tera Expert

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

TalariBalateja_1-1693812986103.png

 

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.

1 ACCEPTED SOLUTION

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 😀

 

View solution in original post

3 REPLIES 3

Ankur Bawiskar
Tera Patron
Tera Patron

@Talari Balateja 

you already have the count right in the variable alertRowCount

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

 

I have the count but I'm not able to push the count to field.

 

Regards,

Sanju.

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 😀