getRefRecord()

the powerful guy!

Posted by Felipe Schmidt on December 25, 2021 · 1 min read

One of the most powerful methods that I don't see many people using: getRefRecord().

When to use it?

  • Let's say you are in a business rule running on "incident" table. In this BR, you need frequently infos from the Caller. Then, the getRefRecord() is awesome.
  • You can also use this method to update records easily and with a few lines of code.

What I usually see people doing it?


  var caller = new GlideRecord('sys_user');
  if (caller.get(current.caller_id.getUniqueValue())){
    caller.active = false;
    caller.update();
  }

It is not wrong, but it is not efficient when you have e.g. a huge code, or you are building a payload as JSON for an API call. In this specific example above, it might look ok to do so, because it is a few lines of code. But if you had many lines in the between and you needed to update something again, you would probably need to do another GlideRecord...

 

How to use it? Assume on code below that the BR runs on "incident", so "current" is your Incident Record. You can save this object in a variable once and reuse it as much as you need in your code anytime you need any info.


  var caller = current.caller_id.getRefRecord();
  caller.active = false; //dot-walk to fields from 'sys_user' table
  caller.update();

Advantages: as you saw, it reduces lots of lines of code, complexity and readability.

Use this official ServiceNow KB Article for more infos.

 

Hope it helps! :-) Happy coding!

Felipe   |   IT Architect