Random "undefined" sys_id returned for an incident record

MarkGlen
Kilo Contributor

Got a situation here that i dont understand, hope you can help.

On the Metric_Instances table we have a requirement to have a reference to the Inicdent table on it, despite there already being an Doc Id field already.

Anyway, for new instances created we are fine as the new field is being populated ok. However we need to populate the field for older records.

I have the following script:

var gr = new GlideRecord('metric_instance');
gr.addQuery('field', "assignment_group");
gr.addQuery('table', "incident");
gr.addQuery('sys_created_on', '>', gs.monthsAgo(3));
gr.query();

gs.log (gr.getRowCount(), "Log1");

while (gr.next()){
gr.u_incident = gr.id;
gr.update();
gs.log(gr.u_incident, "log1");
}

If i run this with the update() commented out it succesfully loops through all 500+ records and logs all the sys-id of the incidents correctly.

When i put the update() in it randomly stops at differrent records and produces a logfile where it indicates that u_incident is "undefined"
It always stop immedatley it encouters an "undefined" u_incident?

Can anyone help me here, i don't understand why it gets this "undefined" error or why it wont just carry on and do the rest of the records?

Thanks,
Mark

2 REPLIES 2

Jim Coyne
Kilo Patron

Try wrapping your script in a function. It could be that another script somewhere else is messing with the "gr" variable.


runScript123();
function runScript123() {
insert code here
}


MarkGlen
Kilo Contributor

That got it working now, thank you.

Will bare that in mind for future when i'm adding new scripts.

Thanks,
Mark