- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-18-2020 04:02 PM
I am trying to script a dynamic dot-walk. To simplify I have included the array of fields that includes a dot-walk in the script below.
var arrExample = ['first_name', 'last_name', 'location.city'];
var grUser = new GlideRecord('sys_user');
grUser.get('e663cd73db3fc7002fdfdd3b5e961970');
for (var i = 0; i < arrExample.length; i++){
// Tried this, did not work
//gs.log(grUser[arrExample[i]]);
// Current workaround
eval('gs.log(grUser.' + arrExample[i] + ');');
}
Current Background Script Output:
*** Script: John
*** Script: Smith
*** Script: Atlanta
I want to get this output without using the eval() statement if possible. Any help would be appreciated!
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-19-2020 11:38 AM
getElement([dotwalk field]) appears to work. I am able to get the desired result without using an eval statement using the script below:
var arrExample = ['first_name', 'last_name', 'location.city'];
var grUser = new GlideRecord('sys_user');
grUser.get('e663cd73db3fc7002fdfdd3b5e961970');
for (var i = 0; i < arrExample.length; i++){
gs.log(grUser.getElement(arrExample[i]));
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-13-2025 09:05 AM
As we are dot-walking the values try using the getElement() method.
Code :
var arrExample = ['first_name', 'last_name', 'location.city'];
var grUser = new GlideRecord('sys_user');
grUser.get('e663cd73db3fc7002fdfdd3b5e961970');
for (var i = 0; i < arrExample.length; i++) {
var element = grUser.getElement(arrExample[i]);
if (element) {
gs.log(arrExample[i] + ': ' + element.getDisplayValue());
} else {
gs.log(arrExample[i] + ': [Invalid field or dot-walk path]');
}
}