GlideRecord to Users table :: "addQuery" not working, but "get" is, Why???

VincentLastname
Tera Expert

I'm using Performance Analytics to report on where tickets were assigned over time using the Metrics table for our Incident tickets. Since those values are stored as a string I'm using a GlideRecord over to our Users [sys_user] table

 

when I try and use addQuery() onto a GlideRecord, it doesn't work: 

var gr = new GlideRecord('sys_user');
gr.addQuery('name', 'Vincent Lastname');
gr.query();
if(gr.hasNext()){
      gr.sys_id;
}

 

 

But when I use get() instead it works:

var gr = new GlideRecord('sys_user');
gr.get('name', 'Vincent Lastname');
gr.sys_id;

 

 

Any idea why this is?? I really want addQuery() to work b/c it's more versatile.

 

(also Insert/Edit Code Sample button wasn't working when formatting this post, I had to directly edit the html to put in the <code> tags. What?)

1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

Instead of hasNext(), you should just use next(). hasNext() does not go to the record but just returns true or false if the a record exists with that query. next() would return a record.

 

 

var gr = new GlideRecord('sys_user');
gr.addQuery('name', 'Vincent Lastname');
gr.query();
if(gr.next()){
      gr.sys_id;
}

 


Please mark this response as correct or helpful if it assisted you with your question.

View solution in original post

2 REPLIES 2

SanjivMeher
Kilo Patron
Kilo Patron

Instead of hasNext(), you should just use next(). hasNext() does not go to the record but just returns true or false if the a record exists with that query. next() would return a record.

 

 

var gr = new GlideRecord('sys_user');
gr.addQuery('name', 'Vincent Lastname');
gr.query();
if(gr.next()){
      gr.sys_id;
}

 


Please mark this response as correct or helpful if it assisted you with your question.

Maddysunil
Kilo Sage

@VincentLastname 

Due to this gr.hasNext() - you were trying to access gr.sys_id without moving the cursor to the next record after performing the query. Below is the correct way to fetch:

 

var gr = new GlideRecord('sys_user');
gr.addQuery('name', 'Vincent Lastname');
gr.query();
if (gr.next()) {
    // Access fields using getFieldValue() or field name
    var sys_id = gr.getValue('sys_id');
    gs.info("User sys_id: " + sys_id);
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks