ServiceNow GlideRecord Bug

It turns out that if you use an invalid field name on a GlideRecord.addQuery() method call, it doesn’t fail (which is what I would have expected it to do). Instead, I runs through all of the records in the underlying table you’ve queried.

That’s annoying in a situation where you’re just querying for informational purposes and you get back several thousand times more records than you were expecting to get. It’s a lot worse than that if you’re in a situation where you’re making an update and you’ve got a typo in the field_name. It will result in an update being made to every record in that table.

Something else to watch for. I always recommend running any update query with a count variable and the update piece commented out first. That lets you confirm that you’re getting the expected number of records back, after which you can go ahead and perform the updates.

A hat tip to my amazing co-worker, Jasmine, who helped figure this out.

Here’s an example of how not to do it:

var gr = new GlideRecord('incident');
gr.addQuery('descriptio', 'Something...');
gr.query();

while(gr.next()) {
	gr.setValue('short_description', 'Redacted');
	gr.update();
}

As you can see, I’ve used ‘descriptio’ instead of ‘description’ in line two.