Fix Script to check table record count and create incident not working

anfield
Tera Guru

Running a fix script against an import table which should create an incident if the table has no records created on that day. I cannot see any of the gs.log logging in the system logs, can anyone help with where I am going wrong?

 

Version - Madrid

 

var queryString = "sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()";

var grq = new GlideRecord('x_sapo_iiq_connect_sysuser');

grq.addEncodedQuery(queryString);
grq.query();

while (grq.next()) {

// gs.addInfoMessage(grq.number);
gs.log("Import Table Records created today: " + grq.getRowCount());


if (grq.getRowCount() > 1) {

gs.log("This is the if loop: " + grq.getRowCount());

var gri = new GlideRecord('incident');

gri.initialize();
gri.caller_id.setValue('27e8b9b9db8023c4039a777a8c96192d');
gri.opened_by('27e8b9b9db8023c4039a777a8c96192d');


gri.contact_type = 'Monitoring';
gri.impact = '3';
gri.urgency = '3';

gri.assignment_group.setValue('f0e98986dbee4700321838ff9d9619f2');


gri.short_description = 'Alert - Import Table - no records';
gri.description = 'Alert - Import Table - no records';
gri.cmdb_ci.setValue('cdc12fb6db9f1f00039a777a8c961934');


gri.u_request_type = 'Service Restoration';
gri.category = 'Interface';
gri.subcategory = 'Sync Issue';
gri.u_owner_group = 'Enterprise Service Desk';

gri.insert();
}

}

1 ACCEPTED SOLUTION

You don't need to loop through records to get the rowCount. You just need to .query() then you can access that property. With your code, in the event that there were say 5 records, you would've made 5 incidents. 

Please mark this as helpful/correct if it resolved your issue!

View solution in original post

7 REPLIES 7

Elijah Aromola
Mega Sage

Should create one incident or many?

Just one for each time the fix script runs, if there are no records in the import table then it should create one incident

Try: 

var queryString = "sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()";
var grq = new GlideRecord('x_sapo_iiq_connect_sysuser');
grq.addEncodedQuery(queryString);
grq.query();
if (grq.getRowCount() == 0) {
    var gri = new GlideRecord('incident');
    gri.initialize();
    gri.caller_id = '27e8b9b9db8023c4039a777a8c96192d';
    gri.opened_by = '27e8b9b9db8023c4039a777a8c96192d';
    gri.contact_type = 'Monitoring';
    gri.impact = '3';
    gri.urgency = '3';
    gri.assignment_group = 'f0e98986dbee4700321838ff9d9619f2';

    gri.short_description = 'Alert - Import Table - no records';
    gri.description = 'Alert - Import Table - no records';
    gri.cmdb_ci = 'cdc12fb6db9f1f00039a777a8c961934';

    gri.u_request_type = 'Service Restoration';
    gri.category = 'Interface';
    gri.subcategory = 'Sync Issue';
    gri.u_owner_group = 'Enterprise Service Desk';

    gri.insert();
}

This works. What do you think was the difference? I see there is no while in your version