Indexing in Glide Record
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2017 05:16 AM
Hi All,
I was working on a script, and I am facing difficulties in looping through the result set. PFB code
var stageUsers = GlideRecord('sys_user');
stageUsers.addQuery('u_first_name','CONTAINS','test');
stageUsers.query();
var userArray = [];
while(stageUsers.next()) {
gs.log(stageUsers.u_first_name);
userArray.push(stageUsers.u_first_name);
}
Assuming 5 users match the criteria: TestUser1,TestUser2,TestUser3,TestUser4,TestUser5
gs.log is printing these 5 users. However, userArray is having 5 entries of TestUser1.
Any idea on how to index the results?
We can save the result to a variable, split it and assign it to an array. But I need to know if we have any method in JavaScript \ ServiceNow OOTB.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2017 01:43 PM
Hi Gowtham,
Try userArray.push(stageUsers.u_first_name.toString()); instead.
Thanks,
Berny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2017 03:10 AM
Hi Berny,
.toString() did work here. However, I am again failing here and can you please help me?
function setSamAccountName(firstname,lastname) {
var tmpFname = firstname;
var tmpLname = lastname;
var fName = "";
var lName = "";
var id = 0;
var samAccountName = "";
fName = firstname.substring(0,3);
lName = lastname.substring(0,3);
samAccountName = fName+lName;
var tmpUser = "";
var tmpIterator="01";
var rowCount=0;
return samAccountName;
}
var stageUsers = GlideRecord('u_ad_bulk_users_staging');
stageUsers.addQuery('u_first_name','CONTAINS','test');
stageUsers.query();
var fNameTmp = '';
var userNames = [];
var lNameTmp = '';
var samAccName = '';
//var description = '';
//workflow.scratchpad.counter = 0;
gs.log(stageUsers.getRowCount());
while(stageUsers.next()) {
var x = stageUsers.u_first_name;
var y = stageUsers.u_last_name;
var z = setSamAccountName(x,y); // Just formats the username like TesUse01
userNames.push(z);
}
gs.log(userNames);
Here, X and Y values are populating properly. but, then it comes to Z, it again fetches the repeated value. I tried appending .toString() too but no luck this time.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2017 06:14 AM
that's interesting.
Have you tried the following?
var x = stageUsers.u_first_name.toString();
var y = stageUsers.u_last_name.toString();
var z = setSamAccountName(x,y); // Just formats the username like TesUse01
userNames.push(z.toString());
Thanks,
Berny
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2017 01:48 PM
the reason why you're looking into 5 entries of the test user1 is because within the push is copying the pointer/reference been used. At the end of iterating through the GlideRecord records you will have all the positions of the array with the same pointer/reference. Doing a .toString() will allow you to case to a string object and will copy the value instead of the reference. Any other approach to cast the value to a string will also work.
Thanks,
Berny