Christopher_Mal
ServiceNow Employee
ServiceNow Employee

I ran into an issue today where a customer was querying for an incident GlideRecord and doing an update. This is pretty common and pretty normal. We do it all the time. Their behavior was not common and anything but normal. Instead of seeing the incident GlideRecord update, we saw all the active incidents update instead.

We looked at the code for a bit and I recognized that the incident GlideRecord was getting initialize() called on it, some values were getting set, and then update() was getting called. Granted we had to traverse through a few methods as this incident GlideRecord was getting passed into other functions, so it wasn't really cut an dry, but I will lay out the issue simpler below.

We are all familiar with initialize() before an insert (and this is fine):



var gr = new GlideRecord('to_do');
gr.initialize();
gr.name = 'first to do item';
gr.description = 'learn about GlideRecord';
gr.insert();


This is not fine


var rec = new GlideRecord('incident');
rec.addQuery('active',true);
rec.query();
while (rec.next()) {
rec.initialize();
rec.active = false;
rec.update();
}


I am not really sure what the system is doing here, and I plan to look through the code a little later, but it produced very strange results where unexpected data was getting updated.

I guess what I am offering as sound advice is don't call the initialize() for anything but an insert().

1 Comment