Glide record query coming back undefined

Michael201
Giga Contributor

The following code is coming back undefined when I try to call it. 

var item = new GlideRecord('sc_req_item');

item.addQuery('request',current.sys_id);
item.query();

gs.addErrorMessage(item.number);

The request field is a reference to the parent of the req item. Iv'e tried both current.sys_id and current.getUniqueValue().

Im just trying to grab the child record from the request. The code seems right to me am I missing something? Thank you

1 ACCEPTED SOLUTION

Uncle Rob
Kilo Patron

A couple things... you need to actually DO something with the gliderecord.   Also, name your variable a little differently, because the sc_req_item table already has a property called "item" on it, so don't want collisions there.

var gritem = new GlideRecord('sc_req_item');
gritem.addQuery('request',current.sys_id);
gritem.query();

while (gritem.next()){
  gs.addErrorMessage(gritem.number);
}

View solution in original post

4 REPLIES 4

kalyani8
Giga Expert

Hi Michael,

 

Please add item.next() and print the value.

 

Refer to the updated code below:

 

var item = new GlideRecord('sc_req_item');

item.addQuery('request',current.sys_id);

item.query();

if(item.next()){

gs.addErrorMessage(item.number);

}

 

Allen Andreas
Administrator
Administrator

Hi,

You'd need to "step" into the actual record the query found by using appropriate format.

I'd recommend checking out the GlideRecord API documentation as you're very close 🙂 -- just need another step.

You'd use

if (item.next()) {
//do x
}

For one record

or

while (item.next()) {
//do x
}

To repeat an action x number of times depending on how many records have been found.

https://developer.servicenow.com/dev.do#!/reference/api/paris/server/no-namespace/c_GlideRecordScope...

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hi Michael,

I'm glad you found the correct answer that worked for you.

I hope the other replies above were also helpful (pretty sure we all said the same thing?).

I also included documentation to the GlideRecord API as well, did that help or would help you?

Thanks and take care! 🙂


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Uncle Rob
Kilo Patron

A couple things... you need to actually DO something with the gliderecord.   Also, name your variable a little differently, because the sc_req_item table already has a property called "item" on it, so don't want collisions there.

var gritem = new GlideRecord('sc_req_item');
gritem.addQuery('request',current.sys_id);
gritem.query();

while (gritem.next()){
  gs.addErrorMessage(gritem.number);
}