Create A GlideRecord . walk from A variable

Markell
Tera Guru

Hi Guys just seeing if this is possible as its failed and brings back undefined so far.

Example Glide Record below

var x = u_name

var grUser = new GlideRecord('sys_user);
grUser.addQuery('sys_id', '122344456666');
grUser.query();
if(grUser.next()){

var example = grUser.x;}

[This is a shortened version of what I'm trying to do by getting the column name and then adding it to the end of the gr Query to be the column name I'm trying to capture data from.]

Thanks in advance

 

 

1 ACCEPTED SOLUTION

Robbie
Kilo Patron
Kilo Patron

Hi Markell,

What you're trying to achieve is possible. I assume you're wanting to use a variable whilst passing through a loop of some sort.

The key with this approach is remembering when using GlideRecord that you're creating an object. With this object you can then dot-walk to fields or columns on the table. Please note however, the column needs to exist, otherwise you're likely to get a response of 'undefined'. See below example with slight tweak to your code.

(Tip - I tested this as a background script on PDI)

var x = 'name'; // column/field that exists on table (eg sys_user)

var grUser = new GlideRecord('sys_user');
grUser.addQuery('sys_id','62826bf03710200044e0bfc8bcbe5df1'); //sys_id of abel.tuter from Personal Dev Instance
grUser.query();
if(grUser.next()){

var example = grUser[x]; //x needs to be a column/field on the table and GlideRecord object. undefined would be returned using your example of x = 'u_name' as u_name does not exist (on a standard sys_user table)

}
gs.print('Example: ' + example);

 

To help others, please mark as correct and/or helpful.

Thanks,

Robbie

View solution in original post

6 REPLIES 6

Anil Lande
Kilo Patron

Hi,

Please try below:

var x = 'u_name';

var grUser = new GlideRecord('sys_user');
grUser.addQuery('sys_id','123456789');
grUser.query();
if(grUser.next()){

var example = grUser[x];  

}

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Also check alternative way:

https://community.servicenow.com/community?id=community_question&sys_id=a53b8024db723410847d5ac2ca96...

var x = 'u_name';

var grUser = new GlideRecord('sys_user');
grUser.addQuery('sys_id', '122344456666');
grUser.query();
if(grUser.next()){

var example = grUser.getValue(x);  

}

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Robbie
Kilo Patron
Kilo Patron

Hi Markell,

What you're trying to achieve is possible. I assume you're wanting to use a variable whilst passing through a loop of some sort.

The key with this approach is remembering when using GlideRecord that you're creating an object. With this object you can then dot-walk to fields or columns on the table. Please note however, the column needs to exist, otherwise you're likely to get a response of 'undefined'. See below example with slight tweak to your code.

(Tip - I tested this as a background script on PDI)

var x = 'name'; // column/field that exists on table (eg sys_user)

var grUser = new GlideRecord('sys_user');
grUser.addQuery('sys_id','62826bf03710200044e0bfc8bcbe5df1'); //sys_id of abel.tuter from Personal Dev Instance
grUser.query();
if(grUser.next()){

var example = grUser[x]; //x needs to be a column/field on the table and GlideRecord object. undefined would be returned using your example of x = 'u_name' as u_name does not exist (on a standard sys_user table)

}
gs.print('Example: ' + example);

 

To help others, please mark as correct and/or helpful.

Thanks,

Robbie

Markell
Tera Guru

Just saying thanks to Both @Anil Lande  and @Robbie as both answers are correct but I can only mark 1 as correct and Robbie provided more in-depth info that helped. Although Anils screenshots helped too. (ServiceNow should allow more than 1 correct answer so as to help with my guilt)