How to get the record count and assign to a variable

anfield
Tera Guru

How do I get a record count, and then push that value to another table? Running a fix script against the location table and I can retrieve the record count using getRowCount, but I can only display that value in the logs. I cannot assign it to a variable or use it to push to another table. I want to push that value to another table. For example

 

var queryString = "sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()";
var grq = new GlideRecord('cmn_location');
grq.addEncodedQuery(queryString);
grq.query();

gs.log('Locations, row count is: ' + grq.getRowCount());
if (grq.getRowCount() > 0) {

gs.log('Locations: Number of Locations created today: ' + grq.getRowCount());


var grq = new GlideRecord('incident');
grq.initialize();
grq.caller_id.setValue('gfgdfgdfgfdgdfgfdg');
grq.opened_by = 'gdfgdfgfdgfsdgdfgfg';
grq.contact_type = 'Monitoring';
grq.impact = '4';
grq.urgency = '4';
grq.location.setValue('ergergergrgwtr);
grq.assignment_group = '5f65b1a01b0bb348fc82773bdc4bcbbd';
grq.short_description = grq.getRowCount() + ' Locations created today';

grq.description = grq.getRowCount() + ' Locations created today';
grq.cmdb_ci = 'cdswefqwefq4553245';
grq.u_request_type = 'Service Restoration';
grq.category = 'Interface';
grq.subcategory = 'Issue';

grq.insert();
}

1 ACCEPTED SOLUTION

It's because you're using the same variable name for both queries. Change that incident query to grq2 instead of grq.

View solution in original post

6 REPLIES 6

It's because you're using the same variable name for both queries. Change that incident query to grq2 instead of grq.

I did that and now it works. Thanks