Use get() to get One or Many Records With GlideRecord

Use get() with ServiceNow

returns many records

How To Use get() in a GlideRecord Query

Simply put, you use get() to return one or more records.

It’s a quick method in a GlideRecord query to return specific records.

Most commonly, you’re using it to retreive a single record.

Most ServiceNow Admins don’t realize that you can actually pass 2 parameters through the method, and return multiple records.

You just have to know the sys_id of the record you want to return.

Let’s look at how to return a single record, if you know the sys_id of the record. Keep in mind that this must be done in a server side script, don’t use this on the client.

Then we’ll look at how to return multiple records, when we don’t have the sys_id.

If you’re a new ServiceNow Admin, check out how to use addEncodedQuery(), to speed up your GlideRecord queries.

Using get() with a sys_id – Return 1 Record

When you know the sys_id of a related record, using get() is a one line addition to a GlideRecord query. There’s no need to add any other methods, as it’s just returning the record that’s being asked for.

This is how you will normally use the get() method. But you have to know the sys_id of the record to “get”.

var gr = new GlideRecord("problem"); // Add the Table

gr.get("sys_id_of_record"); // Add the sys_id, with get();

gr.field_name // You now have access to the record.

Pass 1 parameter, a sys_id, through the get() method to access that record in a server side script.

For example, say we have an incident record and there’s a Problem reference field to that record.

In a query, we can’t easily access the data on that problem record, but since we’re on the Incident – we know the sys_id of the problem record.

Here is the Problem reference field, on the Incident record.

reference field on incident

If we want to interact with that Problem record, while in a query on the incident, we can easily do so using get().

Go to a Background Script and use the following script to do so:

incident gliderecord query

Script:

var inc = new GlideRecord("incident");
inc.addQuery("number", "INC0007001");
inc.query();

if (inc.next()) {
var prbSys = inc.problem_id.toString();

var prb = new GlideRecord("problem");
prb.get(prbSys);

gs.print(prb.number);

}

This is going to be the most common use case (although you’ll likely be doing more than print the number).

Take a look at how you can actually return multiple rows using get(field, value).

Using get() with a Field and Value – Return Multiple Records

So this is closer to using an addQuery() in a query, but you can actually utilize get() to do so.

You don’t need to know the sys_id, if you want to return multiple records.

If you know the field and the value that you want to return – then you can use get().

The syntax looks like the following, where you pass 2 parameters through the method, in the following format get(‘field_name’, field_value);

use get()

The Script:

var inc = new GlideRecord("incident");
inc.addQuery("number", "INC0007001");
inc.query();

if (inc.next()) {
var prbSys = inc.problem_id.toString();

var prb = new GlideRecord("problem");
prb.get("category", "software");
prb.query();

while (prb.next()) {
gs.print(prb.number);
}

}

When you run that get(), you’ll actually get multiple records returned, as stated.

The results look like the following:

background script returns many records

This last one is something that not many people know in the ServiceNow world. Now you’re among the minority of people that know how to pass 2 parameters through the get() method in a GlideRecord query.

Get creative with your use case of this – our example simple prints a record number. But we’ve proven that we can actually access all fields on the related table we’re accessing.

Using get() in ServiceNow is a simple way to retrieve a single, or multiple record(s) with a GlideRecord query.

Let us know below if there are other use cases where you’ve seen get() used before, or if you have any recommendations for us.



What Do You Think About This Article?

4 1 vote
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
James Fricker
James Fricker
10 months ago

3 points I would like to make.

1) If you pass a single argument then it is compared to both the sys_id column and the table’s display column (display=true). for example the display column for the incident table is the number field. So this also works with the single argument method.

var key = ‘INC000001’;
var gr = new GlideRecord(‘incident’);
gr.get(key);
(assuming there is an incident with that number). If you want to ensure that the key only works for the sys_id column you need to use the 2 argument method and specify ‘sys_id’ as the search field.

var key = getSysID();
var gr = new GlideRecord(‘incident’);
gr.get(‘sys_id’, key);
This may sound unimportant but if the search value comes from a system property and you only want sys_ids to work (not some random string) then this is important. For example you don’t want a blank value to find a record with an empty display value.

2) You should always check the boolean return value of get() to ensure you got at least one result before you use it.

3) Think of the get() method as the addQuery(), query() and next() methods all rolled up into a single method. With the boolean return value of next() (true/false) passed as the get() method return value. If you use the query() method straight after the get() method then you are effectively calling query() twice and reseting the record pointer back one, which is slower and inefficient.

var prb = new GlideRecord(“problem”);
if (prb.get(“category”, “software”) {
do {
gs.print(prb.number);
} while (prd.next());
}

Last edited 10 months ago by James Fricker
1
0
Would love your thoughts, please comment.x
()
x