You give GlideRecord a bad name

Do not use “gr” as a variable name!

I’m not the first one telling you this, right?

I hope I’m the last one.

Why should you care?

I’m going to show you with the most basic example possible.

Not some edge case due to a complex workflow triggered by a MID server integration that nobody in your team even knows how it is supposed to work.

Let’s just update the names of some groups.

The groups that have “Database” as their parent.

Let’s prepend “[TEST]” to their name:

var gr = new GlideRecord('sys_user_group');
gr.addQuery('parent','287ee6fea9fe198100ada7950d0b1b73'); //Database
gr.query();
while(gr.next()){
    gr.setValue('name', '[TEST] ' + gr.getValue('name'));
    gr.update();
}
Code language: JavaScript (javascript)

Expected result

Actual result

Why did this happen?

Out of the box technical debt!

There are 52 business rules1 (as of Yokohama) that contain “var gr”, are active and don’t include the “function” keyword.

Among them is the “Auto business rule for Assessments”2, which overrides your variable.

What can you do about it?

  1. Do not use “gr” as a variable name!
  2. Read the following references about better naming conventions and encapsulating your code.

References

Footnotes

  1. https://<instance_name>.service-now.com/sys_script_list.do?sysparm_query=scriptCONTAINSvar gr %3D^active%3Dtrue^scriptDOES NOT CONTAINfunction ↩︎
  2. https://<instance_name>.service-now.com/sys_script.do?sys_id=3ce07524bf3201007a6d257b3f07399f ↩︎