How can we use objects with get() ?

Oya Orhan
Giga Guru

Hi All,

I know the general usage of get().

  var returnValue = grIncident.get('caller_id.name’,’Jane Doe’);

or 

 var returnValue = grIncident.get('57af7aec73d423002728660c4cf6a71c’);

But in the ServiceNow documentation it is defined as:

GlideRecord - get(Object name, Object value)

Product Documentation-GlideRecord - Global

and I never used get() with objects. Have you ever used it? Could you please give me an example?

Thanks

Oya

5 REPLIES 5

Weird
Mega Sage

No idea. Technically when you have a GlideRecord object, you're working with objects when you reference any of the values.

For example typeof gr.number returns object, So gr.get will work with objects when you say something like gr.get('number', gr.number). 

Technically we could say that for a record the short_description says "number" and then if we have previously queried that record (gr) we could do another query saying
"anotherGr.get(gr.short_description, 'Record number here')" and it would work. Or you could also have gr.number instead of string as the value and it would still work.

So I'll just guess they say object because it technically works with objects. It might also just be a mistake or something SN decided to call it since you're not restricted to strings.


Also for example addQuery says "addQuery(String name, Object operator, Object value)". What is an object operator? I don't think I've ever heard about an Object operator in JS, but in this case we always just have a string (like '=') so what's the point of saying anything else than String operator? To my knowledge it's just their own term they've coined since all of the operators aren't technically comparison operators in JS.

 

Oya Orhan1
Kilo Contributor

Hi Joni,

Thanks for your response.

We can use numbers instead of string for query(), too. See the example below please:

var gr = new GlideRecord('incident');
gr.query("state", 2);
var i=0;
gs.print(gr.getRowCount());

But query() input parameters are defined with string in the product documentation as :

GlideRecord - query(String field, String value)
(in the same link in my question)

That's why I think there should be something different with get() if there isn't any mistake in the product documentation.

So, I still have the question mark.

juan9
Giga Guru

Hi Oya,

Here is a code snip that uses the .get() operator:: 

 

//SNIP

var gr = new GlideRecord('incident');
gr.get(sys_id_of_record_here);
//Do something with the record returned
if(gr.category == 'software'){
gs.log('Category is ' + gr.category);
}

 

 

//SNIP 2

 

//Find the first active incident record
var gr = new GlideRecord('incident');
if(gr.get('active', true)){
//Do something with the record returned
gs.log('Category is ' + gr.category);
}

 

snip source:: https://servicenowguru.com/scripting/gliderecord-query-cheat-sheet/

 

if my answer helped, please mark helpful.

If my answer was terrible, let me know where I made the mistake so that I can improve

 

Hitoshi Ozawa
Giga Sage
Giga Sage

On the value side, it's common to use non-String. For example to query for "true/false", number, and date.

Following in an example of using an object in argument name. grUser.company is a reference field and not a string.

var grUser = new GlideRecord('sys_user');
if (grUser.get('62826bf03710200044e0bfc8bcbe5df1')) {
  var grCompany = new GlideRecord('core_company');
  if (grCompany.get(grUser.company)) {  // grUser.company is a reference to the company table
    gs.info(grCompany.name);
  }
}

Execution will provide the company name of the specified user so the script works OK without having to convert a reference field to string.

*** Script: ACME South America